1

hi i want to upload image into the directory and display it using ajax. i want this by using MVC here is my view code. I don't know where i went wrong

<script>
        $(document).ready(function () {
            $("#bt_uploadImage").click(function () {
                if ($("#FileUploader").val() == "") {
                    alert("Please select a file");
                    return false;
                }
                else {
                    debugger;
                    var formData = new FormData();
                    var files = $("#FileUploader").get(0).files;
                    alert(files);
                    // var Files = document.getElementById("FileUploader").files.length;
                    if (files.length > 0) {
                        //   var file = document.getElementById("FileUploader").files[i];
                        formData.append("FileUploader", document.getElementById('FileUploader').files[0]);
                    }
                }
                $.ajax({
                    url: '@Url.Action("create")',
                    type: "POST",
                    data: formData,
                    processData: false,
                    contentType: 'multipart/form-data',
                    success: function (data) {
                        $("#FileUploader").get("src", data);
                    },
                    error: function (er) {
                        alert(er);
                    }
                });
            });
        });
    </script>

and my controller is like this

 if (System.Web.HttpContext.Current.Request.Files.AllKeys.Any())
                     {
                         var file = System.Web.HttpContext.Current.Request.Files["FileUploader"];
                         if (file != null && file.ContentLength > 0)
                         {
                             var fileName = Path.GetFileName(file.FileName);
                             string strFileType = Path.GetExtension(file.FileName).ToString().ToLower();
                             if (strFileType == ".JPEG" && strFileType == ".jpeg" && strFileType == ".JPG" && strFileType == ".jpg" && strFileType == ".png" && strFileType == ".tif" && strFileType == ".tiff")
                             {
                                 string path = Server.MapPath("~/Content/hemanth/");
                                 bool folderExists = Directory.Exists((path));
                                 if (!folderExists)
                                 {
                                     Directory.CreateDirectory((path));
                                     file.SaveAs(path);
                                 }
                             }
                         }
                     }

can u help me where I went wrong..

  • It should be `contentType: false,` –  Oct 14 '15 at 22:09
  • ya I alsoo tried that i didnt got any result more over my controller takes null value @StephenMuecke – hemanth kumar Oct 15 '15 at 04:42
  • is any one there to help me to work out this .. – hemanth kumar Oct 19 '15 at 13:07
  • How can anyone help when you don't even explain what the error is or where its occurring. –  Oct 19 '15 at 21:46
  • when an image ID is sent from View to controller, the controller taking a null value. i menction in my above comment @StephenMuecke – hemanth kumar Oct 20 '15 at 04:51
  • 1
    Then add an answer for the benefit of others or delete the question so others don't waste their time trying to answer. And as a side note, you can simplify your script to just `var formdata = new FormData($('form').get(0));` which will serialize all the files and other form data (refer [this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681)) –  Oct 20 '15 at 04:55

0 Answers0