2013-05-15 59 views
7

我們有4個人爲我們的最終學校項目製作網站。我們需要允許用戶上傳pdf。我們使用Visual Studio 2012,並且設置了一個母版頁,整個登錄過程和用戶​​創建工作。我們使用jQuery和jQueryMobile,因爲該網站也需要爲手機工作,這使得它更容易一些。爲什麼我的ASP Page.Request.Files []總是空的?我已經嘗試過所有發佈的修復程序

但是,當我們想檢查客戶端試圖在我們的代碼中上傳的文件時,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; 
      } 
     } 
    } 
} 
+0

它不會在手機上或在所有的工作? – qwr

+0

沒有。 fileUploadControl.HasFile也總是爲false –

+0

我可以在onchange中爲input type =「file」對象使用javascript方法獲取文件名。但是當它調用後面的代碼時,它們就消失了。 –

回答

15

這是因爲在jQueryMobile中默認使用ajax。在這種情況下,文件上傳不起作用。

所以添加data-ajax="false"來形式應該修復它。

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

In case you are using Razor

一些有用的鏈接:

File upload support on mobile

XMLHttpRequest 2 -Browser support

jQuery File Upload - Browser support

+2

哦,我的上帝!謝謝! 32小時的可怕測試和互聯網。我怎樣才能接受這100次? –

+0

@Ultroman Tacoman歡迎您。很高興我能幫助你。 – qwr

+1

如果您使用剃鬚刀,請檢查:http://stackoverflow.com/questions/17811651/how-do-i-add-data-attributes-to-html-beginform –

相關問題