AdithyaM
AdithyaM

Reputation: 159

File upload without form submit and asynchronously using jQuery and ASP.NET MVC

Upload-Debugging Master Page JQuery Reference

I have a input file control in my asp.net mvc view page as below. I am trying to load file from view to controller without using form and with the help of jQuery ajax.

I am not using any Iframe / FORM in the view page. The file data is not posting to Controller.

System.Web.HttpContext.Current.Request.Files.Count - is always showing as zero. and $("#AttachmenteUploadFile").get(0).files also showing 'undefined' after deploy into stage environment. Is something i am doing wrong in my HTML/JQuery/C#.

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <table id="tableView" border="0">
        <tr>
            <td style="text-align:right;color:black">                   
                <%=LabelHelper.Label("lblFile", "Supplier", "File", Model.User.CultureCode)%>
            </td>
            <td style="text-align:left">
                <input type="file" id="AttachmenteUploadFile" />
                <input type="button" id="btnUploadSubmit" value="Attach Document" />
            </td>
        </tr>             
    </table>
</div>   
</asp:Content>

Here is the JQuery button Onclick event.

$("#btnUploadSubmit").click(function() {
    if (RequiredFieldValidate() != false) {     
    var fileData = new FormData();
    var files = $("#AttachmenteUploadFile").get(0).files;    
    if (FileSizeValidation(filesize) != false) 
    {
        for (var i = 0; i < files.length; i++) {
        fileData.append(files[i].name, files[i]);
    }        
    fileData.append('documentType', $('#ddlDocumentType').val());
    var AttachTypeCode = $('#ddlDocumentType').val();
    $.ajax({
        type: "POST",
        url: getExactPath('/Upload/UploadDocument'),
        data: fileData,
        dataType: 'Json',
        processData: false,
        contentType: false,
        success: function(jsonData) {
            var jsonobj = $.parseJSON(jsonData);
            FillGrid(jsonobj);
        },
        error: function() {
            alert("Unable to upload the Document.");
        }});}}});});

Controller ActionResult from JQuery call.

[HttpPost]
public ActionResult UploadDocument() 
{     
    List < string > mimeType = DocumentumUtil.getMimeTypes();
    if (System.Web.HttpContext.Current.Request.Files.Count > 0)
    {          
        docFullPath = System.Web.HttpContext.Current.Request.Files[0].FileName;
       docFullName = System.Web.HttpContext.Current.Request.Files.AllKeys[0];
       int index = docFullName.LastIndexOf('.');
       docname = docFullName.Substring(0, docFullName.LastIndexOf('.'));
       docExt = docFullName.Substring(index + 1);  
    }  
} catch {
}
return Json(AttachmentDetails, JsonRequestBehavior.AllowGet);
}

some one please help. i'm stuck. Thanks in advance.

Update: Client side the issue resolved, since the FormData is not working in IE (document Mode 7), i have changed to higher version. But still System.Web.HttpContext.Current.Request.Files.Count i am getting zero

Upvotes: 0

Views: 2524

Answers (2)

Amit Sakare
Amit Sakare

Reputation: 214

add this attribute in your form. enctype="multipart/form-data"

and Make sure the input file Name is same as Parameter of Action Method or property of Class

Upvotes: 1

Shyam Vemula
Shyam Vemula

Reputation: 589

Place Name Attribute in design.

<input type="file" id="AttachmenteUploadFile" name="FileUpload_"/>

and Check once.

Upvotes: 0

Related Questions