Ajax在服务器上的本地错误成功

时间:2016-05-26 14:10:32

标签: c# ajax iis file-upload asyncfileupload

我写了一些代码来上传文件。在本地计算机上我收到成功,但它在服务器上以错误结束。这是我的ajax代码:

$.ajax({
            url: '@Url.Action("StartUpload", "Upload")' /*'/Upload/StartUpload'*/,
            type: 'POST',
            data: formdata,
            dataType: 'json',
            contentType: false,
            processData: false,
            success: function (data) {
                for (i = 0; i < data.length; i++) {
                    if (data[i].startsWith("Success"))
                        newAlert('success', data[i] + " Məlumatlari yuklenildi");
                    else
                        newAlert('danger', data[i] + " - Uğursuz nəticələndi");
                }
            },
            error: function (jqXHR) {
                newAlert('danger', jqXHR.statusText + " - Uğursuz nəticələndi (err)");
            }
        });

它上传文件但以错误结束。

C#代码以防万一:

public JsonResult StartUpload()
    {
        ArrayList Act_Result = new ArrayList();

        //var pathS = Links.GetLinks();
        //string BuffUploadPath = "~\\BuffUpload\\";

        for (int i = 0; i < Request.Files.Count; i++)
        {
            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 = file.FileName;
            string mimeType = file.ContentType;
            System.IO.Stream fileContent = file.InputStream;


            file.SaveAs(Server.MapPath(BuffUploadPath) + fileName); //File will be saved in application root
            Act_Result.Add("Success - Server buffere upload edildi. " + BuffUploadPath);          

        }

        if (Act_Result.Count == 0)
            Act_Result.Add("Hec ne icra edilmedi");


        return Json(Act_Result, JsonRequestBehavior.AllowGet);
    }

2 个答案:

答案 0 :(得分:0)

什么错误?

  • 检查file.ContentLength&gt; 0

  • 删除JsonRequestBehavior.AllowGet,因为您使用了ajax POST。

  • 检查路径保存文件。应该使用Server.MapPath(Path.Combine(BuffUploadPath,fileName))

答案 1 :(得分:0)

此代码工作正常。问题不在代码中。我已经解决了问题