asp.net檔案上傳的三種方法

來源:互聯網
上載者:User

asp教程.net檔案上傳的三種方法

<%@ Page language="C#"%>
<%@ Import Namespace="System.IO" %>

<script runat="server">
    void UploadButton_Click(object sender, EventArgs e)
    {
        string savePath = @"c:";
        if (!Directory.Exists(savePath)) {
          string msg = "<h1>The upload path doesn't exist: {0}</h1>";
            Response.Write(String.Format(msg, savePath));
            Response.End();
        }
        if (FileUpload1.PostedFile != null)
        {
            string fileName = Path.GetFileName(FileUpload1.Value);

            savePath += fileName;

          FileUpload1.PostedFile.SaveAs(savePath);
            UploadStatusLabel.InnerText = "File saved as: <i>" + savePath + "</i>";
        }
        else
        {
            UploadStatusLabel.InnerText = "You did not specify a file to upload.";
        }
    }
</script>

<html xmlns="http://www.111cn.net ">
<head runat="server">
    <title>HtmlInputFile</title>
</head>
<body>
    <div id="pageContent">

        <form ID="Form1" runat="server">
          <h3>Select a picture to upload:</h3>
            <hr />
            <b>Picture to upload</b><br />
            <input type="file" id="FileUpload1" runat="server" />
            <br /><br />
            <input runat="server" id="UploadButton" type="submit" value="Upload"
                onserverclick="UploadButton_Click" />
            <hr />
           <span runat="server" id="UploadStatusLabel" />
       </form>
    </div>
</body>
</html>

方法二

<script language="C#" runat="server">
protected void PhotoSubmit(object o, EventArgs e) {
    if(thePhoto.PostedFile != null) {
        try {
            string filepath = "C:temp" + DateTime.Now.Ticks.ToString();
            thePhoto.PostedFile.SaveAs(filepath);
            status.Text = "File saved as " + filepath;
           
        }
        catch (Exception exc) {
            status.Text = "An Error occurred processing the file.  Please try again." + exc.ToString();
        }
    }
}
</script>
<form runat="server" enctype="multipart/form-data">
Please select an image to submit: <input id="thePhoto" type="file" runat="server"><br />
<input type="button" runat="server" value="Proceed" OnServerClick="PhotoSubmit" />
<asp:label runat="server" id="status" />
</form>

方法三

<%@ Page Language="C#" %>
<script runat="server">
void UploadButton_Click(Object sender, EventArgs e) {
    if(FileInput.PostedFile != null)
        {
            try
            {
                FileInput.PostedFile.SaveAs("c:test.txt");
            }
            catch (Exception exc)
            {
                Response.Write(exc.Message);
            }
        }
    }
</script>
<html>
  <head>
  </head>
  <body>
    <form id="Form1" method="post" enctype="multipart/form-data" runat="server">
        <input id="FileInput" type="file" runat="server" />
        <asp:Button id="UploadButton" onclick="UploadButton_Click" runat="server" Text="Upload"></asp:Button>
    </form>
  </body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.