<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文件':) –