Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my ASP Page.Request.Files[] always empty? I've tried all posted fixes
    primarykey
    data
    text
    <p>We're 4 guys making a website for our final school project. We need to allow a user to upload a pdf. We're using Visual Studio 2012, and we have a Master page set up, and the whole login-process and user-creation works. We're using jQuery, and also jQueryMobile because the site needs to work for phones as well, and this makes it a bit easier.</p> <p>But when we want to check the files the client is trying to upload in our code behind, Request.Files is always empty. Why is this?</p> <p>I have the enctype set in the form I'm using, so I should be fine. It looks like the page reloads when I click the upload-button, and the text-field in the file-input gets cleared. Then after this, it runs the code behind method.</p> <p>The output we get is:</p> <pre><code>HALLOOOOOOOOOOOOOO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! In the house Files[] size is 0 </code></pre> <p>The Master page that looks like this:</p> <pre><code> &lt;%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Main.Master.cs" Inherits="SendEtBrev.Main" %&gt; &lt;!DOCTYPE html&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head runat="server"&gt; &lt;title&gt;SendEtBrev.dk&lt;/title&gt; &lt;meta http-equiv="Pragma" content="no-cache" /&gt; &lt;meta http-equiv="Expires" content="-1" /&gt; &lt;meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no;" /&gt; &lt;meta name="MobileOptimized" content="width" /&gt; &lt;meta name="HandheldFriendly" content="true" /&gt; &lt;link rel="stylesheet" type="text/css" href="/Styles/reset.css" /&gt; &lt;script src="http://code.jquery.com/jquery-1.9.1.min.js"&gt;&lt;/script&gt; &lt;script src="/Js/fixes.js"&gt;&lt;/script&gt; &lt;script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"&gt;&lt;/script&gt; &lt;link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" /&gt; &lt;link rel="stylesheet" type="text/css" href="/Styles/sendetbrev.css" /&gt; &lt;asp:ContentPlaceHolder ID="head" runat="server" /&gt; &lt;/head&gt; &lt;body&gt; &lt;header&gt; &lt;div class="headerbox"&gt; &lt;div class="headerlogo"&gt; &lt;img class="autosizedimage" src="/Billeder/Logo.png" /&gt; &lt;/div&gt; &lt;asp:LoginView ID="LoginViewMenu" runat="server"&gt; &lt;LoggedInTemplate&gt; &lt;a href="/Account/Minkonto.aspx"&gt;Min konto&lt;/a&gt; &lt;a href="/Account/Logout.aspx"&gt;Log ud&lt;/a&gt; &lt;/LoggedInTemplate&gt; &lt;/asp:LoginView&gt; &lt;br /&gt; &lt;/div&gt; &lt;/header&gt; &lt;br /&gt; &lt;br /&gt; &lt;div&gt; &lt;asp:ContentPlaceHolder ID="centercontent" runat="server" /&gt; &lt;/div&gt; &lt;footer&gt; &lt;/footer&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>My ASPX code is this:</p> <pre><code>&lt;%@ Page Language="C#" MasterPageFile="~/Main.Master" ValidateRequest = "False" AutoEventWireup="true" CodeBehind="Side1Uploadfil.aspx.cs" Inherits="SendEtBrev.SendBrev.Side1Uploadfil" %&gt; &lt;asp:Content ID="Content1" ContentPlaceHolderID="centercontent" runat="server" &gt; &lt;asp:Literal ID="introText" runat="server"/&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:Literal ID="AccepteredeFormater" runat="server" /&gt; &lt;br /&gt; .pdf&lt;br /&gt; &lt;!-- vises kun hvis der er en fejlbesked ved upload --&gt; &lt;asp:Literal ID="errorMessage" runat="server" EnableViewState="false" /&gt;&lt;br /&gt; Select a file to upload: &lt;form id="form1" name="form1" method="post" runat="server" enctype="multipart/form-data" &gt; &lt;input type="file" accept="*.pdf" id="fileUploadControl" name="fileUploadControl" runat="server" /&gt; &lt;asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" /&gt; &lt;br/&gt;&lt;br/&gt; &lt;br /&gt; &lt;/form&gt; &lt;asp:Button ID="FortsaetKnap" runat="server" data-role="none" CssClass="knap1" Visible="False" OnClientClick="javascript:location.replace('/SendBrev/Side2Modtager.aspx');" /&gt;&lt;br /&gt; &lt;/asp:Content&gt; </code></pre> <p>And my code behind is this:</p> <pre><code>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 &gt; 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 &gt; 1 &amp;&amp; !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 &gt; 0) { FortsaetKnap.Visible = true; } } } else { Debug.WriteLine("Filestream is null"); Console.WriteLine("Filesream is null"); Response.Write("Filestream is null"); FortsaetKnap.Visible = false; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload