【问题标题】:Upload File using form not posting the file使用不发布文件的表单上传文件
【发布时间】:2014-01-11 20:33:40
【问题描述】:
    <form id="UploadForm" action="UploadFileServer.axd" method="post" enctype="multipart/form-data" runat="server">
        <div class="folderSelectorCont">
            <div class="select_file_ttl">${resource.upload_file_Select_File}:</div>
            <div class="folderNameInputArea">

                <button class="select_file_btn" id="choose_file">Select</button>
                <input type="file" class="hidden_input" id="file"/> 
                <label class="chosen_folder" id="file_name" title=""></label>

            </div>
        </div>
        <div class="popButtonsCont">
            <a class="dialogBtns" onclick="hideDialog(); return false;">${resource.manageFolder_cancel}</a>
            <a class="dialogBtns" id="createBtn">${resource.manageFolder_ok}</a>
        </div>
    </form>

上传文件服务器.axd:

    void IHttpHandler.ProcessRequest(HttpContext ctx)
    {

        HttpFileCollection uploadFile = ctx.Request.Files;
        if (uploadFile.Count > 0)
        {
                      //do something


        ctx.Response.ContentType = "application/json; charset=utf-8";
        ctx.Response.Write(uploadFileResponse);
    }

但我得到 uploadFile.Count = 0。 为什么?

【问题讨论】:

  • 您是否在页面中使用任何 javascript 库?
  • 我特别想知道您是否正在使用 jQuery mobile,因为 answer 在 OP 使用 jQuery mobile 时修复了类似的问题。
  • 是的,我正在使用 jquery mobile
  • 没有帮助,现在它根本没有到达服务器端
  • data-ajax=false 添加到表单后,它就无法访问服务器?

标签: c# asp.net forms file-upload


【解决方案1】:

正如我在 cmets 中提到的,已知 jQuery mobile 在这种情况下会导致问题,请将此另一个问题视为example。更新您的配置以关闭 jQuery Mobile 的 ajax,然后它应该可以工作了。

【讨论】:

    【解决方案2】:

    正如这个人指出的那样:http://www.prolistingservice.com/theblog/post/HttpFileCollection-Always-Zero.aspx

    摘录:

    我发现的每个网站都说,要填充 HttpFileCollection 需要做两件事。

    1) 确保表单方法是“POST” 2) 确保表单编码类型为“multipart/form-data”

    AjaxFileUpload 代码尽职尽责地拥有这个权利,但是 ASP.NET 还需要做一件事。

    3) HTML 文件输入必须具有“名称”属性。

    我已经使用您提供的代码创建了一个测试项目,并注册了 HttpHandler,并且在我将“name”属性添加到输入元素后它就可以工作了。

    这里是代码,注意 input 元素中添加了 name 属性:

    Web.config:

    <configuration>
        <system.web>
          <compilation debug="true" targetFramework="4.5" />
          <httpRuntime targetFramework="4.5" />
        </system.web>
        <system.webServer>
          <handlers>
            <add name="uploadHandler" path="UploadFileServer.axd" verb="*" type="FileUploadHandler.IISHandler1, FileUploadHandler" />
          </handlers>
        </system.webServer>
    </configuration>
    

    HTTP 处理程序:

    namespace FileUploadHandler
    {
        public class IISHandler1 : IHttpHandler
        {
            public bool IsReusable { get { return false; } }
    
            void IHttpHandler.ProcessRequest(HttpContext ctx)
            {
    
                HttpFileCollection uploadFile = ctx.Request.Files;
                string uploadFileResponse = "no count";
                if (uploadFile.Count > 0)
                {
                    uploadFileResponse = "count > 0";
                }
    
                ctx.Response.ContentType = "application/json; charset=utf-8";
                ctx.Response.Write(uploadFileResponse);
            }
        }
    }
    

    页面:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
            <form id="UploadForm" action="UploadFileServer.axd" method="post" enctype="multipart/form-data" runat="server">
            <div class="folderSelectorCont">
                <div class="select_file_ttl">${resource.upload_file_Select_File}:</div>
                <div class="folderNameInputArea">
    
                    <button class="select_file_btn" id="choose_file">Select</button>
                    <input type="file" class="hidden_input" id="file" name="file"/> 
                    <label class="chosen_folder" id="file_name" title=""></label>
    
                </div>
            </div>
            <div class="popButtonsCont">
                <a class="dialogBtns" onclick="hideDialog(); return false;">${resource.manageFolder_cancel}</a>
                <a class="dialogBtns" id="createBtn">${resource.manageFolder_ok}</a>
            </div>
        </form>
    </body>
    </html>
    

    【讨论】:

    • 谢谢 asm00,但我试过了,但没有帮助,你能告诉我你的测试代码吗?
    • 非常感谢您的示例和解释。问题出在 jquery mobile .js 文件上。
    【解决方案3】:

    当发布表单数据时,控件必须具有名称属性,因为数据以名称-值对的形式发送,因此首先尝试在输入中添加名称:

    <input type="file" class="hidden_input" id="file" name="file" />
    

    【讨论】:

    • 谢谢,但没有帮助:(
    猜你喜欢
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 2016-05-01
    • 2011-08-12
    • 2015-11-13
    • 1970-01-01
    相关资源
    最近更新 更多