2010-07-02 113 views
6

我想製作一個服務器頁面(C#,asp.net 2.0+)來保存從另一個頁面上傳的文件。ASP.NET文件上傳

具體來說, 我有一個

<form action="upload.aspx"> 

一個HTML頁面,我無法弄清楚如何處理節省upload.aspx服務器上的文件。

我發現了幾個例子(其中一個是:http://msdn.microsoft.com/en-us/library/aa479405.aspx) 但這需要<input type=file>元素在同一頁面上。

我在抓取upload.aspx頁面上發佈的文件時遇到困難。

任何人有任何指針?如何在aspx中抓取張貼的文件並在文件從其他頁面發佈時將其保存到服務器?

非常感謝, 佈雷特與此類似,然後將其寫入到磁盤

回答

10

1.創建Uploadfile.aspx

2.Embed的Uploadfile.aspx使用iframe

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Uploadfile.aspx.cs" Inherits="Uploadfile" %> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head runat="server"> 
     <title>File Upload Control</title> 
    </head> 
    <body> 
     <form id="form1" runat="server"> 
     <div> 
     <asp:FileUpload runat="server" ID="fuSample" /> 
     <asp:Button runat="server" ID="btnUpload" Text="Upload" 
       onclick="btnUpload_Click" /> 
       <asp:Label runat="server" ID="lblMessage" Text=""></asp:Label> 
     </div> 
     </form> 
    </body> 
    </html> 

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"; 
    } 
} 

你的HTML頁面,然後嵌入您的aspx頁面在HTML作爲跟進,

<iframe height="40" width="700" src="Uploadfile.aspx"> 
</iframe> 

現在你可以可以使用UploadFiles.aspx從HTML本身上傳文件。

1

使用的代碼(使用,比如說文件流)

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

你不能做到這一點沒有<input type=file">

<form action="upload.aspx">不發送文件告訴服務器在哪裏發送請求估計。

0

我做了一個小的測試用例:

  1. Uploader.aspx標記:從Upload.aspx

    <form id="form1" runat="server"> 
    <div> 
        <asp:FileUpload runat="server" ID="fuTest" /><br /> 
        <asp:Button runat="server" ID="btnUpload" Text="Upload" PostBackUrl="~/Uploading/Upload.aspx" /> 
    </div> 
    </form> 
    
  2. 代碼隱藏:

    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,並從中檢索您想要的內容。