用户界面
<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
,但我怀疑这是否正确?
但是我如何在没有任何开销的情况下满足我的要求呢? 内存分配、执行和所有这些都非常关键