0

I have wrote some code to upload files. On local computer I receive success, but it end with error on server. Here is my ajax code:

$.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)");
            }
        });

It uploads the file but end with error.

C# code just in case:

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);
    }
Huseyn Zeynalov
  • 123
  • 2
  • 9

2 Answers2

0

What error?

  • Check file.ContentLength > 0.

  • Remove JsonRequestBehavior.AllowGet because you use ajax POST.

  • Check path save file. Should use Server.MapPath(Path.Combine(BuffUploadPath,fileName))

Thanh Binh
  • 40
  • 4
  • 1) I have Checked file.ContentLength > 0. It is true. 2) I have removed sonRequestBehavior.AllowGet 3) I have used Server.MapPath(Path.Combine(BuffUploadPath,fileName)) instead of Server.MapPath(BuffUploadPath) + fileName Still have error – Huseyn Zeynalov May 27 '16 at 05:40
0

This code works fine. Problem wasn't in code. I have solved problem

Huseyn Zeynalov
  • 123
  • 2
  • 9