8

我们是 4 个人为我们的期末学校项目制作网站。我们需要允许用户上传 pdf。我们使用的是 Visual Studio 2012,我们设置了一个母版页,整个登录过程和用户​​创建工作正常。我们使用 jQuery 和 jQueryMobile,因为该网站也需要在手机上运行,​​这使它更容易一些。

但是当我们想在我们后面的代码中检查客户端试图上传的文件时,Request.Files 总是空的。为什么是这样?

我以我正在使用的形式设置了 enctype,所以我应该没问题。当我单击上传按钮时,页面似乎重新加载,并且文件输入中的文本字段被清除。然后在此之后,它运行方法背后的代码。

我们得到的输出是:

HALLOOOOOOOOOOOOOO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
In the house
Files[] size is 0

主页面如下所示:

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Main.Master.cs" Inherits="SendEtBrev.Main" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
      <title>SendEtBrev.dk</title>

      <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Expires" content="-1" />
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no;" />
        <meta name="MobileOptimized" content="width" />
        <meta name="HandheldFriendly" content="true" />

        <link rel="stylesheet" type="text/css" href="/Styles/reset.css" />
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script src="/Js/fixes.js"></script>
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
        <link rel="stylesheet" type="text/css" href="/Styles/sendetbrev.css" />   

      <asp:ContentPlaceHolder ID="head" runat="server" />


  </head>
  <body>
      <header>
        <div class="headerbox">
          <div class="headerlogo">
            <img class="autosizedimage" src="/Billeder/Logo.png" />
          </div>
              <asp:LoginView ID="LoginViewMenu" runat="server">
                 <LoggedInTemplate>                    
                   <a href="/Account/Minkonto.aspx">Min konto</a>
                   <a href="/Account/Logout.aspx">Log ud</a>               
                 </LoggedInTemplate>
              </asp:LoginView>
          <br />
        </div>

      </header>

      <br />
      <br />
      <div>
          <asp:ContentPlaceHolder ID="centercontent" runat="server" />
      </div>
      <footer>

      </footer>
  </body>
</html>

我的 ASPX 代码是这样的:

<%@ Page Language="C#" MasterPageFile="~/Main.Master" ValidateRequest = "False" AutoEventWireup="true" CodeBehind="Side1Uploadfil.aspx.cs" Inherits="SendEtBrev.SendBrev.Side1Uploadfil" %>

<asp:Content ID="Content1" ContentPlaceHolderID="centercontent" runat="server" >

  <asp:Literal ID="introText" runat="server"/>
  <br />
  <br />
  <asp:Literal ID="AccepteredeFormater" runat="server" />
  <br />
  .pdf<br />



  <!-- vises kun hvis der er en fejlbesked ved upload -->
  <asp:Literal ID="errorMessage" runat="server" EnableViewState="false" /><br />



    Select a file to upload:
      <form id="form1" name="form1" method="post" runat="server" enctype="multipart/form-data" >
          <input type="file" accept="*.pdf" id="fileUploadControl" name="fileUploadControl" runat="server" />
        <asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" />

        <br/><br/>

    <br />
      </form>
  <asp:Button ID="FortsaetKnap" runat="server" data-role="none" CssClass="knap1" Visible="False"
      OnClientClick="javascript:location.replace('/SendBrev/Side2Modtager.aspx');" /><br />


</asp:Content>

我背后的代码是这样的:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using System.IO;

namespace SendEtBrev.SendBrev
{
    public partial class Side1Uploadfil : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            introText.Text = "Du er nu klar til at sende et brev.";
            AccepteredeFormater.Text = "Følgende filformater accepteres:";
            FortsaetKnap.Text = "Fortsæt";
            btnUpload.Text = "Upload";
        }

        protected void btnUploadClick(object sender, EventArgs e)
        {
            Response.Write("HALLOOOOOOOOOOOOOO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            Console.WriteLine("HALLOOOOOOOOOOOOOO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            Debug.WriteLine("HALLOOOOOOOOOOOOOO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

            if (fileUploadControl != null)
            {
                Response.Write("In the house");
                Console.WriteLine("In the house");
                Debug.WriteLine("In the house");
            }

            if (Page.Request.Files.Count > 0)
            {
                {
                    //Get the first file. There could be multiple if muti upload is supported
                    string fileName = Page.Request.Files[0].FileName;

                    //Some validation
                    if (Page.Request.Files[0].ContentLength > 1 && !string.IsNullOrEmpty(fileName))
                    {
                        FileValidator(Page.Request.Files[0].InputStream);
                    }
                }
            }
            else
            {
                Debug.WriteLine("Files[] size is 0");
                Console.WriteLine("Files[] size is 0");
                Response.Write("Files[] size is 0");
            }

        }

        protected void FileValidator(Stream myFileStream)
        {
            Debug.WriteLine("Running FileValidator...");
            Console.WriteLine("Running FileValidator...");
            Response.Write("Running FileValidator...");
            if (myFileStream != null)
            {
                using (StreamReader sr = new StreamReader(myFileStream))
                {
                    Regex regex = new Regex(@"/Type\s*/Page[^s]");
                    MatchCollection matches = regex.Matches(sr.ReadToEnd());
                    Console.Write("PDF'en har " + matches.Count + " sider");
                    Debug.Write("PDF'en har " + matches.Count + " sider");
                    Response.Write("PDF'en har " + matches.Count + " sider");
                    if (matches.Count > 0)
                    {
                        FortsaetKnap.Visible = true;
                    }
                }
            }
            else
            {
                Debug.WriteLine("Filestream is null");
                Console.WriteLine("Filesream is null");
                Response.Write("Filestream is null");
                FortsaetKnap.Visible = false;
            }
        }
    }
}
4

1 回答 1

16

这是因为在 jQueryMobile 中默认使用 ajax。在这种情况下,文件上传不起作用。

所以添加data-ajax="false"到表单应该可以解决它。

 <form id="form1" name="form1"  data-ajax="false" method="post" runat="server"
 enctype="multipart/form-data" >

如果您使用的是 Razor

一些有用的链接:

移动端文件上传支持

XMLHttpRequest 2 - 浏览器支持

jQuery 文件上传 - 浏览器支持

于 2013-05-15T21:33:46.830 回答