ASP.Net/Asp Control/FileUpload
Содержание
FileUpload Control
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>FileUpload Control</title>
</head> <body>
<form id="form1" runat="server">
FileUpload Control
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" /> <asp:Button ID="btnDisplay" runat="server" Text="Display" OnClick="btnDisplay_Click" />
<asp:Label ID="lblMessage" runat="server" /> <asp:Label ID="lblDisplay" runat="server" />
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; public partial class Default_aspx : System.Web.UI.Page {
protected void btnSave_Click(object sender, EventArgs e){ string str = ""; if (FileUpload1.HasFile) { try { str += "Uploading file: " + FileUpload1.FileName; FileUpload1.SaveAs("c:\\" + FileUpload1.FileName); str += "
Saved As: " + FileUpload1.PostedFile.FileName; str += "
File Type: " + FileUpload1.PostedFile.ContentType; str += "
File Length (bytes): " + FileUpload1.PostedFile.ContentLength; str += "
PostedFile File Name: " + FileUpload1.PostedFile.FileName; } catch (Exception ex) { str += "
Error
Unable to save c:\\" + FileUpload1.FileName + "
" + ex.Message; } } else { str = "No file uploaded."; } lblMessage.Text = str; lblDisplay.Text = ""; } protected void btnDisplay_Click(object sender, EventArgs e) { string str = "File: " + FileUpload1.FileName + "
"; if (FileUpload1.HasFile) { try { Stream stream = FileUpload1.FileContent; StreamReader reader = new StreamReader(stream); string strLine = ""; do { strLine = reader.ReadLine(); str += strLine; } while (strLine != null); } catch (Exception ex) { str += "
Error
Unable to display " + FileUpload1.FileName + "
" + ex.Message; } } else { str = "No file uploaded."; } lblDisplay.Text = str; lblMessage.Text = ""; }
}
</source>
Get file name
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="FileUpload" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
Upload File
<asp:FileUpload ID="fupAspNetUpload" runat="server" /><input id="fupHtmlUpload" runat="server" type="file" />
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />
</form>
</body> </html> File: Default.aspx.cs using System; using System.Configuration; using System.Collections; using System.Data; using System.IO; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class FileUpload : System.Web.UI.Page {
protected void btnUpload_Click(object sender, EventArgs e) { string htmlFilePath = fupHtmlUpload.PostedFile.FileName; string aspNetFilePath = fupAspNetUpload.PostedFile.FileName; string filePath = fupAspNetUpload.FileName; string fileName = Path.rubine(Server.MapPath("."), filePath); fupAspNetUpload.SaveAs(fileName); }
}
</source>
Uploads to a special upload folder
<source lang="csharp">
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Default_aspx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit_Click" />
</form>
</body> </html> File: Default.aspx.vb
Partial Class Default_aspx
Inherits System.Web.UI.Page Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim savePath As String = Request.PhysicalApplicationPath savePath += "uploads\" If FileUpload1.HasFile Then " verify if there is file to upload savePath += FileUpload1.FileName " existing file will be overwritten FileUpload1.SaveAs(savePath) Response.Write("File uploaded successfully!") Else Response.Write("No file to upload") End If End Sub
End Class
</source>