ASP.NET文件上载

我正在尝试创建一个服务器页面(C#,asp.net 2.0+)来保存从另一个页面上传的文件。

具体来说,我有一个HTML页面

我无法弄清楚如何处理在upload.aspx中保存服务器上的文件。

我找到了一些例子(一个是: http : //msdn.microsoft.com/en-us/library/aa479405.aspx ),但这需要元素在同一页面上。

我在upload.aspx页面上抓取已发布的文件时遇到了困难。

任何人有任何指针? 当从另一个页面发布文件时,如何在aspx中获取已发布的文件并将其保存到服务器?

非常感谢,布雷特

1.创建Uploadfile.aspx

2.使用iframe在您的Html页面中嵌入Uploadfile.aspx

  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Uploadfile.aspx.cs" Inherits="Uploadfile" %>    File Upload Control    

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Uploadfile : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnUpload_Click(object sender, EventArgs e) { //Files is folder Name fuSample.SaveAs(Server.MapPath("Files") + "//" + fuSample.FileName); lblMessage.Text = "File Successfully Uploaded"; } } 

然后将您的aspx页面嵌入到Html中,如下所示,

  

现在,您可以使用UploadFiles.aspx从html本身上传文件。

使用与此类似的代码,然后将其写入磁盘(使用,例如,FileStream)

 HttpFileCollection MyFileCollection; HttpPostedFile MyFile; int FileLen; System.IO.Stream MyStream; MyFileCollection = Request.Files; MyFile = MyFileCollection[0]; FileLen = MyFile.ContentLength; byte[] input = new byte[FileLen]; // Initialize the stream. MyStream = MyFile.InputStream; // Read the file into the byte array. MyStream.Read(input, 0, FileLen); 

http://msdn.microsoft.com/en-us/library/system.web.httppostedfile.inputstream%28VS.71%29.aspx

没有你不能这样做

不发送文件告诉服务器将请求发送到哪里。

我做了一个小测试用例:

  1. Uploader.aspx标记:

     

  2. 来自Upload.aspx的Codebehind:

     protected void Page_Load(object sender, EventArgs e) { FileUpload fu = PreviousPage.FindControl("fuTest") as FileUpload; if (fu != null) { int length = fu.PostedFile.ContentLength; } } 

按钮的PostBackUrl属性将其发布到Upload.aspx页面。 在那里,您可以使用Page类的PreviousPage属性从上一页中查找控件,将其强制转换为FileUpload,并从中检索您想要的内容。