How to save a file to disk?

Here is my controller:

[HttpPost] public ActionResult Index(HttpPostedFileBase excelFile) { /*Somewhere here, I have to save the uploaded file.*/ var fileName = string.Format("{0}\\{1}", Directory.GetCurrentDirectory(), excelFile.FileName); var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName); var adapter = new OleDbDataAdapter("SELECT * FROM [workSheetNameHere$]", connectionString); var ds = new DataSet(); adapter.Fill(ds, "results"); DataTable data = ds.Tables["results"]; return View(); } 
+4
source share
3 answers

This is one way to handle it if you get downloaded files.

 string nameAndLocation = "~/UploadedFiles/" + hpf.FileName; hpf.SaveAs(Server.MapPath(nameAndLocation)); 
+11
source

Have you tried the HttpPostedFileBase.SaveAs method?

+5
source
 [HttpPost] public ActionResult Index(HttpPostedFileBase excelFile) { /*Somewhere here, I have to save the uploaded file.*/ var fileName = string.Format("{0}\\{1}", Directory.GetCurrentDirectory(), excelFile.FileName); excelFile.SaveAs(fileName ); //... } 

If in doubt, check out the documentation: http://msdn.microsoft.com/en-us/library/system.web.httppostedfilebase.saveas.aspx

+3
source

Source: https://habr.com/ru/post/1341241/


All Articles