user4059149
user4059149

Reputation: 25

Request.Files always 0

<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
   
   <table id="tblAttachment">  </table>

    <input id="btnSubmit" type="button" value="button" />
    </form>
</body>

Dynamically inserting a FileUpload Control

<script>
        $(document).ready(function () {
            var MaxAttachment = 1;
            $("#tblAttachment").append('<tr><td><input id=\"Attachment_' + MaxAttachment.toString() + '\" name=\"file\" type=\"file\" /><br><a class=\"MoreAttachment\">Additional Attachment</a></td></tr>');

            $("#btnSubmit").on("click", UploadFile);
        });

         
    </script>

Sending Data to .ashx using Jquery

 function UploadFile() {
              var kdata = new FormData();

              var i = 0;
              //run through each row
              $('#tblAttachment tr').each(function (i, row) {

                  var row = $(row);

                  var File = row.find('input[name*="file"]');

                  alert(File.val());

                  kdata.append('file-' + i.toString(), File);
                  i = i + 1;

              });

              sendFile("fUpload.ashx", kdata, function (datad) { 
              }, function () { alert("Error in Uploading File"); }, true);

               
          }

On .ashx Count always Zero ??

public class fUpload : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        int k = context.Request.Files.Count;
        context.Response.Write(UploadMultipleFiles());
    }

Ajax Request

function sendFile(requestUrl, dataPayload, successfunc, errorfunc, synchronousMode) { 
    $.ajax({
        url: requestUrl,
        type: "POST",
        dataType: "json",

        contentType: false,
        processData: false,
        cache: false,
        async: synchronousMode,
        data: dataPayload,
        success: successfunc,
        error: errorfunc
    });
}

Please Check .. where i m doing wrong

form tag having enctype="multipart/form-data" and each fileUPload control have uniue id and name attribute too

Thanks

Upvotes: 1

Views: 2926

Answers (1)

adeneo
adeneo

Reputation: 318182

You're sending a jQuery object, not a file ?

Shortened down, you're basically doing this

var kdata = new FormData();

var File = row.find('input[name*="file"]'); // jQuery object

kdata.append('file-0', File);

You need the files, not the jQuery objects

var kdata = new FormData();

var File  = row.find('input[name*="file"]'); // jQuery object

var files = File.get(0).files[0]; // it not multiple

kdata.append('file-0', Files);

Upvotes: 1

Related Questions