在mvc控制器中使用xmlhttp上传文件request.files.count为零即将进入ie

时间:2014-09-11 12:55:45

标签: asp.net-mvc

var formdata = new FormData(); //FormData object
var fileInput = document.getElementById('fileToUploadinproposal');

for (i = 0; i < fileInput.files.length; i++) {

    formdata.append(fileInput.files[i].name, fileInput.files[i]);
}

//Creating an XMLHttpRequest and sending
var xhr = new XMLHttpRequest();
xhr.open('POST', '/UploadFile/UploadProposalDocument/' + 105);
xhr.send(formdata);
xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && xhr.status == 200) {
        var path = xhr.responseText;
        self.ViewDocuments(true);
        alert(xhr.responseText);
    }
}
return false;

[HttpPost]
public ActionResult UploadProposalDocument(int id)
{

    string uploadedPath = string.Empty;
    bool flag;
    bool pathindb;
    for (int i = 0; i < Request.Files.Count; i++)
    {
        HttpPostedFileBase file = Request.Files[i]; 

        Guid guId = Guid.NewGuid();

        int fileSize = file.ContentLength;
        string fileName = Path.GetFileName(file.FileName);
        fileName = guId + "_" + fileName;

        string mimeType = file.ContentType;
        var path = Path.Combine(Server.MapPath("~/Content/Upload"), fileName);
        fileName = Path.GetFileName(file.FileName);
        byte[] fileData = null;
        using (var binaryReader = new BinaryReader(file.InputStream)) { fileData = binaryReader.ReadBytes(file.ContentLength); }

        file.SaveAs(path); //File will be saved in application root
    }
    return Json(uploadedPath);
}      

它不会进入循环内部。我在一个poc中完成它使用相同的代码,但我存在的项目它不会进入for循环,请给出任何解决方案。

0 个答案:

没有答案