如何将文件保存到磁盘?

4 c# file save

这是我的控制器:

    [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();
    }
Run Code Online (Sandbox Code Playgroud)

Jac*_*tti 11

如果您正在接收上传的文件,这是处理它的一种方法.

string nameAndLocation = "~/UploadedFiles/" + hpf.FileName;
hpf.SaveAs(Server.MapPath(nameAndLocation));
Run Code Online (Sandbox Code Playgroud)


Art*_*nko 6

你试过HttpPostedFileBase.SaveAs方法吗?