Ajax FileUpload & JQuery formData in ASP.NET MVC

前端 未结 3 1129
轮回少年
轮回少年 2020-12-13 21:26

I have some problem with posting formData to server side action method. Because ajax call doesn\'t send files to server, I have to add file uploader data to

相关标签:
3条回答
  • 2020-12-13 21:51

    Instead of Jquery Ajax you could use

     <script>
                function SubmitButtonOnclick()
                { 
                    var formData= new FormData();
                    var imagefile=document.getElementById("imageFile").files[0];
                    var coverfile=document.getElementById("coverFile").files[0];
                    formData.append("imageFile",imageFile);
                    formData.append("coverFile",coverFile);
                    var xhr = new XMLHttpRequest();
                    xhr.open("POST", "/Profile/EditProfile", true);
                    xhr.addEventListener("load", function (evt) { UploadComplete(evt); }, false);
                    xhr.addEventListener("error", function (evt) { UploadFailed(evt); }, false);
                    xhr.send(formData);
    
                }
    
          function UploadComplete(evt) {
            if (evt.target.status == 200) 
                    alert("Logo uploaded successfully.");
    
            else 
                     alert("Error Uploading File");
            }
    
        function UploadFailed(evt) {
            alert("There was an error attempting to upload the file.");
    
        }
     </script>
    

    This works for me !!

    Your script with changes

     function SubmitButtonOnclick() {
            var formData = new FormData();
            var file = document.getElementById("imageFile").files[0];
            var file1 = document.getElementById("coverFile").files[0];
            formData.append("imageFile", file);
            formData.append("coverfile", file1);
            $.ajax({
                type: "POST",
                url: '@Url.Action("EditProfile","Default1")',
                data: formData,
                dataType: 'json',
                contentType: false,
                processData: false,
                success: function (response) {
                    $('#GeneralSection').html(response.responseText);
                },
                error: function (error) {
                    $('#GeneralSection').html(error.responseText);
                }
            });
        }
    
    0 讨论(0)
  • 2020-12-13 22:07
       <input type="file" id="picfile" name="picf" />
           <input type="text" id="txtName" style="width: 144px;" />
     $("#btncatsave").click(function () {
    var Name = $("#txtName").val();
    var formData = new FormData();
    var totalFiles = document.getElementById("picfile").files.length;
    
                        var file = document.getElementById("picfile").files[0];
                        formData.append("FileUpload", file);
                        formData.append("Name", Name);
    
    $.ajax({
                        type: "POST",
                        url: '/Category_Subcategory/Save_Category',
                        data: formData,
                        dataType: 'json',
                        contentType: false,
                        processData: false,
                        success: function (msg) {
    
                                     alert(msg);
    
                        },
                        error: function (error) {
                            alert("errror");
                        }
                    });
    
    });
    
     [HttpPost]
        public ActionResult Save_Category()
        {
          string Name=Request.Form[1]; 
          if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files[0];
             }
    
    
        }
    
    0 讨论(0)
  • 2020-12-13 22:11

    I was able to upload file through jquery using the iframe, I explained the same in my blog post, please follow the link to get the answer.

    http://kaushikghosh12.blogspot.com/2014/02/ajax-fileupload-on-all-browsers.html

    0 讨论(0)
提交回复
热议问题