2013-11-28 22 views
4

我需要爲我的項目使用這個plugin,但它似乎只在將文件輸入放入原始代碼時正常工作。但是,當我嘗試從進行動態生成的文件輸入,context.Request.Files.Count總是返回0如何使ajaxfileupload與動態生成的文件輸入一起工作

這是我作爲指導article

這就是我如何努力去適應那篇文章我的需求:

ASPX和jQuery:

<%@ Page Title="Página principal" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> 

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> 
    <script src="Scripts/jquery-1.10.2.js" type="text/javascript"></script> 
    <script src="Scripts/ajaxfileupload.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $("#btnGenerateFileInputs").click(function() { 
       var body = $("#tbDatos > tbody"); 
       for (var i = 1; i <= 10; i++) { 
        var row = $("<tr>"); 
        var col = $("<td>"); 
        col.append(
        "<div><input id='fupFile_" + i + "' type='file' accept='application/vnd.openxmlformats-officedocument.SpreadsheetML.Sheet'/>" + 
        "<input id='btnUpload_" + i + "' type='button' value='Upload' />" + 
        "</div>" 
        ); 
        row.append(col); 
        body.append(row); 
        add_clickUploadFile("btnUpload_" + i); 
       } 
      }); 
      function add_clickUploadFile(elemId) { 
       elem = $("#" + elemId);   
       elem.on('click', function() { 
        var idFileUpload = $(this).prev().attr("id"); 
        $.ajaxFileUpload({ 
         url: 'AjaxFileUploader.ashx', 
         secureuri: false, 
         fileElementId: idFileUpload, 
         dataType: 'json', 
         success: function (data, status) { 
          if (typeof (data.error) != 'undefined') { 
           if (data.error != '') { 
            alert(data.error); 
           } else { 
            alert(data.msg); 
           } 
          } 
         }, 
         error: function (data, status, e) { 
          alert(e); 
         } 
        }); 
        return false;      
       }); 
      }    
     }); 
    </script> 
</asp:Content> 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
    <input id="btnGenerateFileInputs" type="button" value="Generate File Inputs" /> 
    <table id="tbDatos" style="width: 100%;"> 
     <tbody> 
     </tbody> 
    </table> 
</asp:Content> 

處理程序:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.IO; 

namespace jQueryFileUpload 
{ 

    public class AjaxFileuploader : IHttpHandler 
    { 

     public void ProcessRequest(HttpContext context) 
     { 
      if (context.Request.Files.Count > 0) 
      { 
       string path = context.Server.MapPath("~/Temp"); 
       if (!Directory.Exists(path)) 
        Directory.CreateDirectory(path); 

       var file = context.Request.Files[0]; 

       string fileName; 

       if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE") 
       { 
        string[] files = file.FileName.Split(new char[] { '\\' }); 
        fileName = files[files.Length - 1]; 
       } 
       else 
       { 
        fileName = file.FileName; 
       } 
       string strFileName = fileName; 
       fileName = Path.Combine(path, fileName); 
       file.SaveAs(fileName); 


       string msg = "{"; 
       msg += string.Format("error:'{0}',\n", string.Empty); 
       msg += string.Format("msg:'{0}'\n", strFileName); 
       msg += "}"; 
       context.Response.Write(msg); 


      } 
     } 

     public bool IsReusable 
     { 
      get 
      { 
       return true; 
      } 
     } 
    } 
} 

製品和我上面發佈的代碼中的示例的主要區別如下:

代替直接當主叫JavaScript函數創建按鈕我決定這樣做:

enter image description here

然後當按鈕被點擊時,調用處理程序,傳遞當前文件輸入的id。

enter image description here

正如我告訴你之前,調用處理程序,但context.Request.Files.Count總是返回0,因爲如果沒有選中任何文件。

任何想法,爲什麼會發生這種情況?

一如既往的任何意見或指導將不勝感激。

在此先感謝。

回答

0

關於我可以在您的代碼和文章的代碼中看到的差異是您的輸入字段缺少「name」屬性。因此,嘗試改變下面的代碼:

col.append(
    "<div><input id='fupFile_" + i + "' type='file' accept='application/vnd.openxmlformats-officedocument.SpreadsheetML.Sheet'/>" + 
    "<input id='btnUpload_" + i + "' type='button' value='Upload' />" + 
    "</div>" 
); 

col.append(
    "<div><input id='fupFile_" + i + "' name='fupFile_" + i + "' type='file' accept='application/vnd.openxmlformats-officedocument.SpreadsheetML.Sheet'/>" + 
    "<input id='btnUpload_" + i + "' name='btnUpload_" + i + "' type='button' value='Upload' />" + 
    "</div>" 
); 
相關問題