为什么我的ASP Page.Request.Files []总是空的?我已经尝试了所有补丁发布 [英] Why is my ASP Page.Request.Files[] always empty? I've tried all posted fixes
问题描述
我们是4人作出了我们的最终学校项目网站。我们需要允许用户上传PDF。我们使用Visual Studio 2012,我们有一个母版页设置,整个登录过程和用户创作的作品。我们使用jQuery,也jQueryMobile因为该网站需要为电话以及工作,这使得它更容易一些。
但是,当我们要检查的客户端试图在我们的code上传背后的文件,Request.Files总是空空的。这是为什么?
我有ENCTYPE在我使用的形式设置,所以我应该罚款。它看起来像重新加载页面,当我点击上传按钮,并在文件输入文本字段被清除。然后在此之后,它运行的背后方法code。
我们得到的输出是:
HALLOOOOOOOOOOOOOO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
在家里
文件[]大小为0
母版页,看起来像这样:
<%@主语言=C#AutoEventWireup =真codeBehind =Main.Master.cs继承=SendEtBrev.Main%GT;<!DOCTYPE HTML>< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<标题> SendEtBrev.dk< /标题> < META HTTP-EQUIV =语用内容=无缓存/>
< META HTTP-EQUIV =过期CONTENT = - 1/>
< META NAME =视口CONTENT =WIDTH =设备的宽度;初始规模= 1.0;最大规模= 1.0;用户可扩展性=无; />
< META NAME =MobileOptimizedCONTENT =宽度/>
< META NAME =HandheldFriendlyCONTENT =真/> <链接rel =stylesheet属性类型=文/ CSS的href =/风格/ reset.css/>
&所述; SCRIPT SRC =HTTP://$c$c.jquery.com/jquery-1.9.1.min.js>&下; /脚本>
<脚本的src =/ JS / fixes.js>< / SCRIPT>
&所述; SCRIPT SRC =HTTP://$c$c.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js>&下; /脚本>
<链接rel =stylesheet属性HREF =HTTP://$c$c.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css/>
<链接rel =stylesheet属性类型=文/ CSS的href =/风格/ sendetbrev.css/> < ASP:的ContentPlaceHolder ID =头=服务器/>
< /头>
<身体GT;
<报头GT;
< DIV CLASS =headerbox>
< DIV CLASS =headerlogo>
&所述; IMG类=autosizedimageSRC =/ Billeder / Logo.png/>
< / DIV>
< ASP:LoginView ID =LoginViewMenu=服务器>
<&显示LoggedInTemplate GT;
< A HREF =/帐号/ Minkonto.aspx>最低KONTO< / A>
< A HREF =/帐号/ Logout.aspx>登录UD< / A>
< /&显示LoggedInTemplate GT;
< / ASP:LoginView>
< BR />
< / DIV> < /头> < BR />
< BR />
< DIV>
< ASP:的ContentPlaceHolder ID =centercontent=服务器/>
< / DIV>
<页脚GT&; < /页脚>
< /身体GT;
< / HTML>
我的ASPX code是这样的:
<%@页面语言=C#的MasterPageFile =〜/ Main.MasterValidateRequest =FALSEAutoEventWireup =真codeBehind =Side1Uploadfil.aspx的.cs继承=SendEtBrev.SendBrev.Side1Uploadfil%GT;< ASP:内容ID =内容1ContentPlaceHolderID =centercontent=服务器> < ASP:文字ID =introText=服务器/>
< BR />
< BR />
< ASP:文字ID =AccepteredeFormater=服务器/>
< BR />
.PDF< BR /> <! - 台钳坤hvis德呃恩fejlbesked粘弹性阻尼器上传 - >
< ASP:文字ID =的errorMessage=服务器的EnableViewState =假/>< BR /> 选择要上传的文件:
<表ID =form1的NAME =Form1的方法=邮报=服务器ENCTYPE =的multipart / form-data的>
<输入类型=文件接受=* PDF。ID =fileUploadControlNAME =fileUploadControl=服务器/>
< ASP:按钮=服务器ID =btnUpload的OnClick =btnUploadClick文本=上传/> < BR />< BR /> < BR />
< /表及GT;
< ASP:按钮的ID =FortsaetKnap=服务器数据角色=无的CssClass =knap1可见=假
的OnClientClick =JavaScript的:location.replace('/ SendBrev / Side2Modtager.aspx'); />< BR />
< / ASP:内容>
和我的code的背后是这样的:
使用系统;
使用System.Collections.Generic;
使用System.Diagnostics程序;
使用System.Linq的;
使用的System.Web;
使用System.Web.Script.Services;
使用System.Web.UI程序;
使用System.Web.UI.HtmlControls;
使用iTextSharp.text.pdf;
使用iTextSharp.text.xml;
使用System.Text.RegularEx pressions;
使用System.Web.UI.WebControls;
使用System.IO;命名空间SendEtBrev.SendBrev
{
公共部分类Side1Uploadfil:System.Web.UI.Page
{ 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
introText.Text =杜儿女KLAR直到在森德等brev。
AccepteredeFormater.Text =Følgendefilformater accepteres:
FortsaetKnap.Text =Fortsæt;
btnUpload.Text =上传;
} 保护无效btnUploadClick(对象发件人,EventArgs的发送)
{
的Response.Write(HALLOOOOOOOOOOOOOO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!);
Console.WriteLine(HALLOOOOOOOOOOOOOO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!);
的Debug.WriteLine(HALLOOOOOOOOOOOOOO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!); 如果(fileUploadControl!= NULL)
{
的Response.Write(在家里);
Console.WriteLine(在家里);
的Debug.WriteLine(在家里);
} 如果(Page.Request.Files.Count大于0)
{
{
//获取第一个文件。如果支持穆蒂载可能有多个
字符串文件名= Page.Request.Files [0] .FileName; //一些验证
如果(Page.Request.Files [0] .ContentLength> 1安培;&安培;!string.IsNullOrEmpty(文件名))
{
FileValidator(Page.Request.Files [0] .InputStream);
}
}
}
其他
{
的Debug.WriteLine(文件[]大小为0);
Console.WriteLine(文件[]大小为0);
回复于(文件[]大小为0);
} } 保护无效FileValidator(流myFileStream)
{
的Debug.WriteLine(运行FileValidator ......);
Console.WriteLine(运行FileValidator ......);
的Response.Write(运行FileValidator ......);
如果(myFileStream!= NULL)
{
使用(StreamReader的SR =新的StreamReader(myFileStream))
{
正则表达式的regex =新的正则表达式(@/型号\\ S * /页[^ S]);
MatchCollection匹配= regex.Matches(sr.ReadToEnd());
Console.Write(哈日PDF'en+ matches.Count +代尔);
Debug.Write(哈日PDF'en+ matches.Count +代尔);
的Response.Write(哈日PDF'en+ matches.Count +代尔);
如果(matches.Count大于0)
{
FortsaetKnap.Visible = TRUE;
}
}
}
其他
{
的Debug.WriteLine(文件流为空);
Console.WriteLine(Filesream为空);
的Response.Write(文件流为空);
FortsaetKnap.Visible = FALSE;
}
}
}
}
这是因为Ajax是默认使用的jQueryMobile。并在这种情况下,文件上传不起作用。
所以加入数据AJAX =FALSE
来形成应该修复它。
<表ID =form1的NAME =Form1的数据AJAX =false的方法=邮报=服务器
ENCTYPE =的multipart / form-data的>
一些有用的链接:
移动文件上传的支持
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.
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?
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.
The output we get is:
HALLOOOOOOOOOOOOOO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
In the house
Files[] size is 0
The Master page that looks like this:
<%@ 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>
My ASPX code is this:
<%@ 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>
And my code behind is this:
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;
}
}
}
}
It happens because ajax is used by default in jQueryMobile. And on that case file upload does not work.
So Adding data-ajax="false"
to form should fix it.
<form id="form1" name="form1" data-ajax="false" method="post" runat="server"
enctype="multipart/form-data" >
Some useful links:
XMLHttpRequest 2 -Browser support
jQuery File Upload - Browser support
这篇关于为什么我的ASP Page.Request.Files []总是空的?我已经尝试了所有补丁发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!