<input type = "file" title="Please upload some file" name="file" />
MVC
/*below method is called from a MVC controller
this method resides for now in MVC project itself*/
public IEnumerable<CustomerTO> FetchJson(HttpPostedFile file)
{
using (StreamReader sr = new StreamReader(file.FileName))
{
{
file.saveAs("details.txt");
string json = sr.ReadToEnd();
IEnumerable<CustomerTO> customers=
JsonConvert.DeserializeObject<List<CustomerTO>>(json);
return customers;
}
}
}
当MVC项目上面的方法或某种基于Web的项目中所有的引用都是找的罚款。
但我想创建一个实用程序类来处理所有这些操作。 所以我创建&添加类Utitlity.cs
类库
public IEnumerable<CustomerTO> FetchJson(HttpPostedFile file)
{
//but HttpPostedFile is throwing error.
//NOTE Ideally,I shouldn't be saving the posted file
}
一个类库项目现在我知道的FileUpload是UI控制HttpPostedFile处理与此相关的所有操作。
我可以很容易地添加引用using System.Web
,但我怀疑这是对的吗?
但是,如何解决我的需求,然后没有任何形式的开销? 内存分配,执行&所有这些是非常关键的
不太确定如何声明第一个作品 - 您的文件输入没有'name'属性,因此参数将为'null'。你又如何调用第二个“实用程序”方法 - 你的Controller中仍然需要一个操作方法来接收请求。 –
@StephenMuecke,对不起!添加了缺少的细节 –
它需要'name =“file”'绑定到一个参数'HttpPostedFile文件':) –