如何在本地将上传的文件数据保存到文本文件中?的WebAPI

时间:2014-07-22 07:37:29

标签: asp.net-mvc file-upload knockout.js asp.net-web-api

我有一个文件上传控件,一旦我浏览文件(可能是图像/文本)并点击保存意味着该文件应该保存到我的本地计算机中的某个文件夹。

我正在尝试:

self.save =function(){

var formdata = new FormData(); //FormData object

var opmlFile = $('#FFtoSP')[0]; // i get my entire uploaded file details 
formdata.append("opmlFile", opmlFile.files[0]); // i am appending to formdata

 // Now my formdata holds the data and now how come i save it in text file in my local machine dynamiclly 

};

我的观点:

<form>
 <input name="file" type="file" id="FFtoSP"  />
 <input type="button" id=""  value="save" data-bind="click:$data.save" /> 
</form>

我的控制器代码:

[HttpPost]
public ActionResult Upload(string id)
{

    string uploadedPath = string.Empty;
    for (int i = 0; i < Request.Files.Count; i++) // i get count zero for IE and one for other browsers
    {
        HttpPostedFileBase file = Request.Files[i]; //Uploaded file

        //Use the following properties to get file's name, size and MIMEType
        int fileSize = file.ContentLength;
        string fileName = Path.GetFileName(file.FileName);
        fileName = "Contract" + "_" + "CreateContract" + "_" + fileName;
        string mimeType = file.ContentType;
        var path = Path.Combine(Server.MapPath("~/Content/Upload"), fileName);//path where i want to save


        //To save file, use SaveAs method
        file.SaveAs(path); //File will be saved in application root 


    }
    return Json(uploadedPath);

}

任何线索都可以提供很好的帮助

0 个答案:

没有答案