Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to upload a file using jquery and it work fine with text file but when i try to upload a pdf file it gives an error and always return 0 to Request.Files.Count.

i try for 2 hours and dont understand the error please help!

What I have tried:

Client Side Code

HTML


<input id="file" class="form-control-file" type="file" name="file" placeholder="Document Upload" />



Jquery

$("#file").change(function () {
    var formData = new FormData();
    var totalFiles = document.getElementById("file").files.length;
    for (var i = 0; i < totalFiles; i++) {
        var file = document.getElementById("file").files[i];

        formData.append("file", file);
    }
    $.ajax({
        type: "POST",
        url: '/Admin/PreviewUpload',
        data: formData,
        dataType: 'json',
        contentType: false,
        processData: false,
        success: function (response) {
            alert('succes!!');
        },
        error: function (error) {
            alert("errror");
        }
    });
});


Server Side Code

[HttpPost]   
  public ActionResult PreviewUpload()
    {
        if (Request.Files.Count > 0)
        {
            foreach (string files in Request.Files)
            {
                var _file = Request.Files[files];
                FileInfo Fi = new FileInfo(Path.GetFileName(_file.FileName));
                string fileExtention = Fi.Extension;
                if (_file != null)
                {
                    if (fileExtention == ".PDF")
                    {
                        string fileName = Path.GetFileName(_file.FileName);
                        if (_file.ContentLength <= 120000000)
                        {
                            _file.SaveAs(Server.MapPath("~/PreviewPDF/" + fileName));
                        }
                        string path = "/PreviewPDF/" + Path.GetFileName(_file.FileName);
                        ViewData["error"] = path;
                        return Json(new
                        {
                            Success = path
                        });
                    }
                    else
                    {
                        return Json(new
                        {
                            fileError = "Only Support PDF"
                        });
                    }
                }
                else
                {
                    return Json(new
                    {
                        error = "Please Select the file"
                    });
                }
            }
        }
        return Json(new
        {
            error = "Please Select the file"
        });
    }
Posted
Comments
Richard Deeming 14-Nov-19 8:14am    
Try removing dataType: 'json' from your AJAX call.
tyson Hamda 14-Nov-19 8:44am    
thanks bro for your help but the problem was it allow only 4 mb file and I try to upload 22 mb file for test and waste my 2 hours :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900