0

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

3
  • Have u tried with the below answer Commented May 18, 2019 at 4:49
  • yes i tried. no luck. I am not sure what is the issue here. Commented May 19, 2019 at 15:17
  • Try @Amit Sakare answer. Commented May 21, 2019 at 1:59

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

you mean the controller action name and input file name should be same?
0

Place Name Attribute in design.

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

and Check once.

3 Comments

i tried the same, it is not working. when i create new asp.net-mvc project the i am able to get the file upload control information, i am using this in one of my existing asp.net mvc application. where this functionality is not working. Is something i am missing any JQuery references? I am using Master page, it is referring the jquery references. please see the attachment image
That is not related to the JQuery references I think.
when i start the work i referred "cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" and "code.jquery.com/ui/1.12.1/jquery-ui.js" in view page, And it worked in local. But in stage the script getting failed. all the dev work is done and i am stuck here before move to prod.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.