Request.Files始终为0

时间:2014-09-19 16:29:24

标签: c# jquery asp.net

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

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

动态插入FileUpload控件

<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>

使用Jquery将数据发送到.ashx

 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始终为零??

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

Ajax请求

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
    });
}

请检查..我在哪里做错了

具有enctype =“multipart / form-data”的表单标记,并且每个fileUPload控件也具有uniue id和name属性

由于

1 个答案:

答案 0 :(得分:1)

您发送的是jQuery对象,而不是文件?

缩短了,你基本上就是这样做了

var kdata = new FormData();

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

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

您需要文件,而不是jQuery对象

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);