ASP.Net/Asp Control/FileUpload

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

FileUpload Control

<%@ 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">
    <div>
    <h1>FileUpload Control</h1>
     <asp:FileUpload ID="FileUpload1" runat="server" />
     <br />
     <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
     <asp:Button ID="btnDisplay" runat="server" Text="Display" OnClick="btnDisplay_Click" />
     <br />
     <br />
     <asp:Label ID="lblMessage" runat="server" />
     <asp:Label ID="lblDisplay" runat="server" />
    </div>
    </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 += "<br/>Saved As: " + FileUpload1.PostedFile.FileName;
         str += "<br/>File Type: " + FileUpload1.PostedFile.ContentType;
         str += "<br/>File Length (bytes): " + FileUpload1.PostedFile.ContentLength;
         str += "<br/>PostedFile File Name: " + FileUpload1.PostedFile.FileName;
       } catch (Exception ex) {
         str += "<br/><b>Error</b><br/>Unable to save c:\\" + FileUpload1.FileName + "<br/>" + ex.Message;
       }
     } else {
       str = "No file uploaded.";
     }
     lblMessage.Text = str;
     lblDisplay.Text = "";
   }
  protected void btnDisplay_Click(object sender, EventArgs e)
  {
    string str = "<u>File:  " + FileUpload1.FileName + "</u><br/>";  
    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 += "<br/><b>Error</b><br/>Unable to display " + FileUpload1.FileName + "<br/>" + ex.Message;
      }
    }
    else
    {
      str = "No file uploaded.";
    }
    lblDisplay.Text = str;
    lblMessage.Text = "";
  }
}



Get file name

<%@ 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">
    <div>
        <h1>
            Upload File</h1>
        <asp:FileUpload ID="fupAspNetUpload" runat="server" />
        <br />
        <br />
        <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />
        <br />
        <br />
        <input id="fupHtmlUpload" runat="server" type="file" /></div>
    </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);
    }
}



Uploads to a special upload folder

<%@ 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">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit"  OnClick="Submit_Click" /><br />
        <br />
    
    </div>
    </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