国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET - FileUpload Web 服務器控件概述(下)

2019-11-17 04:01:50
字體:
來源:轉載
供稿:網友
示例

第一個示例演示如何創建 FileUpload 控件,該控件將文件保存到代碼中指定的路徑。

<%@ Page Language="C#" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<script runat="server">



  PRotected void UploadButton_Click(object sender, EventArgs e)

  {

    // Specify the path on the server to

    // save the uploaded file to.

    String savePath = @"c:/temp/uploads/";



    // Before attempting to perform Operations

    // on the file, verify that the FileUpload

    // control contains a file.

    if (FileUpload1.HasFile)

    {

      // Get the name of the file to upload.

      String fileName = FileUpload1.FileName;



      // Append the name of the file to upload to the path.

      savePath += fileName;





      // Call the SaveAs method to save the

      // uploaded file to the specified path.

      // This example does not perform all

      // the necessary error checking.               

      // If a file with the same name

      // already exists in the specified path,  

      // the uploaded file overwrites it.

      FileUpload1.SaveAs(savePath);



      // Notify the user of the name of the file

      // was saved under.

      UploadStatusLabel.Text = "Your file was saved as " + fileName;

    }

    else

    {      

      // Notify the user that a file was not uploaded.

      UploadStatusLabel.Text = "You did not specify a file to upload.";

    }



  }

</script>



<html  >

<head runat="server">

    <title>FileUpload Example</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

       <h4>Select a file to upload:</h4>



       <asp:FileUpload id="FileUpload1"                 

           runat="server">

       </asp:FileUpload>



       <br /><br />



       <asp:Button id="UploadButton"

           Text="Upload file"

           OnClick="UploadButton_Click"

           runat="server">

       </asp:Button>    



       <hr />



       <asp:Label id="UploadStatusLabel"

           runat="server">

       </asp:Label>        

    </div>

    </form>

</body>

</html>



      第二個示例演示如何創建 FileUpload 控件,該控件將文件保存到文件系統中針對應用程序的指定目錄。

<%@ Page Language="C#" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<script runat="server">



    protected void UploadButton_Click(object sender, EventArgs e)

    {

        // Save the uploaded file to an "Uploads" directory

        // that already exists in the file system of the

        // currently executing asp.net application.  

        // Creating an "Uploads" directory isolates uploaded

        // files in a separate directory. This helps prevent

        // users from overwriting existing application files by

        // uploading files with names like "Web.config".

        string saveDir = @"/Uploads/";



        // Get the physical file system path for the currently

        // executing application.

        string appPath = Request.PhysicalApplicationPath;



        // Before attempting to save the file, verify

        // that the FileUpload control contains a file.

        if (FileUpload1.HasFile)

        {

            string savePath = appPath + saveDir +

                Server.HtmlEncode(FileUpload1.FileName);



            // Call the SaveAs method to save the

            // uploaded file to the specified path.

            // This example does not perform all

            // the necessary error checking.               

            // If a file with the same name

            // already exists in the specified path,  

            // the uploaded file overwrites it.

            FileUpload1.SaveAs(savePath);



            // Notify the user that the file was uploaded successfully.

            UploadStatusLabel.Text = "Your file was uploaded successfully.";



        }

        else

        {

            // Notify the user that a file was not uploaded.

            UploadStatusLabel.Text = "You did not specify a file to upload.";   

        }

    }

</script>



<html  >

<head runat="server">

    <title>FileUpload Class Example</title>

</head>

<body>

    <h3>FileUpload Class Example: Save To Application Directory</h3>

    <form id="form1" runat="server">

    <div>

       <h4>Select a file to upload:</h4>



       <asp:FileUpload id="FileUpload1"                 

           runat="server">

       </asp:FileUpload>



       <br/><br/>



       <asp:Button id="UploadButton"

           Text="Upload file"

           OnClick="UploadButton_Click"

           runat="server">

       </asp:Button>    



       <hr />



       <asp:Label id="UploadStatusLabel"

           runat="server">

       </asp:Label>           

    </div>

    </form>

</body>

</html>



      第三個示例演示如何創建 FileUpload 控件,該控件將文件保存到指定路徑并限制可以上載的文件的大小。

<%@ Page Language="C#" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<script runat="server">



    protected void UploadButton_Click(object sender, EventArgs e)

    {

        // Specify the path on the server to

        // save the uploaded file to.

        string savePath = @"c:/temp/uploads/";



        // Before attempting to save the file, verify

        // that the FileUpload control contains a file.

        if (FileUpload1.HasFile)

        {                

            // Get the size in bytes of the file to upload.

            int fileSize = FileUpload1.PostedFile.ContentLength;



            // Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded.

            if (fileSize < 2100000)

            {



                // Append the name of the uploaded file to the path.

                savePath += Server.HtmlEncode(FileUpload1.FileName);



                // Call the SaveAs method to save the

                // uploaded file to the specified path.

                // This example does not perform all

                // the necessary error checking.               

                // If a file with the same name

                // already exists in the specified path,  

                // the uploaded file overwrites it.

                FileUpload1.SaveAs(savePath);



                // Notify the user that the file was uploaded successfully.

                UploadStatusLabel.Text = "Your file was uploaded successfully.";

            }

            else

            {

                // Notify the user why their file was not uploaded.

                UploadStatusLabel.Text = "Your file was not uploaded because " +

                                         "it exceeds the 2 MB size limit.";

            }

        }   

        else

        {

            // Notify the user that a file was not uploaded.

            UploadStatusLabel.Text = "You did not specify a file to upload.";

        }

    }

</script>



<html  >

<head runat="server">

    <title>FileUpload Class Example</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

       <h4>Select a file to upload:</h4>



       <asp:FileUpload id="FileUpload1"                 

           runat="server">

       </asp:FileUpload>



       <br/><br/>



       <asp:Button id="UploadButton"

           Text="Upload file"

           OnClick="UploadButton_Click"

           runat="server">

       </asp:Button>



       <hr />



       <asp:Label id="UploadStatusLabel"

           runat="server">

       </asp:Label>



    </div>

    </form>

</body>

</html>



第四個示例演示如何創建 FileUpload 控件,該控件將文件保存到指定路徑并且只允許上載擴展名為 .doc 或 .xls 的文件。

<%@ Page Language="C#" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<script runat="server">



    protected void UploadBtn_Click(object sender, EventArgs e)

    {

        // Specify the path on the server to

        // save the uploaded file to.

        string savePath = @"c:/temp/uploads";



        // Before attempting to save the file, verify

        // that the FileUpload control contains a file.

        if (FileUpload1.HasFile)

        {

            // Get the name of the file to upload.

            string fileName = Server.HtmlEncode(FileUpload1.FileName);



            // Get the extension of the uploaded file.

            string extension = System.IO.Path.GetExtension(fileName);



            // Allow only files with .doc or .xls extensions

            // to be uploaded.

            if ((extension == ".doc") | (extension == ".xls"))

            {

                // Append the name of the file to upload to the path.

                savePath += fileName;



                // Call the SaveAs method to save the

                // uploaded file to the specified path.

                // This example does not perform all

                // the necessary error checking.               

                // If a file with the same name

                // already exists in the specified path,  

                // the uploaded file overwrites it.

                FileUpload1.SaveAs(savePath);



                // Notify the user that their file was successfully uploaded.

                UploadStatusLabel.Text = "Your file was uploaded successfully.";

            }

            else

            {

                // Notify the user why their file was not uploaded.

                UploadStatusLabel.Text = "Your file was not uploaded because " +

                                         "it does not have a .doc or .xls extension.";

            }



        }



    }



</script>



<html  >

<head runat="server">

    <title>FileUpload Class Example</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <h4>Select a file to upload:</h4>



        <asp:FileUpload id="FileUpload1"                 

            runat="server">

        </asp:FileUpload>



        <br/><br/>



        <asp:Button id="UploadBtn"

            Text="Upload file"

            OnClick="UploadBtn_Click"

            runat="server">

        </asp:Button>    



        <hr />



        <asp:Label id="UploadStatusLabel"

            runat="server">

        </asp:Label>     

    </div>

    </form>

</body>

</html>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长武县| 元朗区| 武义县| 都昌县| 新竹县| 茶陵县| 余干县| 亳州市| 东兰县| 永济市| 桐庐县| 田东县| 铜川市| 罗城| 云龙县| 思茅市| 郯城县| 桐柏县| 江山市| 盐源县| 泰宁县| 富川| 沭阳县| 淅川县| 盐边县| 镇雄县| 盱眙县| 缙云县| 太仓市| 胶南市| 临颍县| 德安县| 海门市| 德昌县| 分宜县| 杭州市| 嵩明县| 永靖县| 平和县| 九台市| 吉安县|