C# 文件上傳

 1、分析

      本次博客,主要解決文件上傳等一系列問題,將從兩方面來論述,即1G之內文件和1G以上文件。html

      對於上傳1G之內的文件,能夠採用基本的三種上傳方法:用Web控件FileUpload、html控件HtmlInputFile和用Html元素<input type="file" id="file"/>,經過Request.Files上傳。前端

      對於1G以上的大文件,思路爲:web

           (1)協議:可採用http協議或ftp協議後端

           (2)斷點續傳學習

           (3)使用插件ui

           (4)非插件形式實現this

2、文件大小屬於[0,1G]範圍

     html控件HtmlInputFile實現上傳:spa

      一、上傳界面插件

     

     二、前端代碼 debug

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUpLoad.aspx.cs" Inherits="DEMO.FileUpLoad" %>

 
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Image ID="Image1" runat="server"/>
       <asp:Button ID="btnUpLoad" runat="server" Text="上傳" OnClick="btnUpLoad_Click" />  
       <asp:Label ID="Label1" runat="server" Text="" Style="color: Red"></asp:Label>  
    </div>
    </form>
</body>
</html>

  三、後端代碼  

 //string serverpath = Server.MapPath("~/ImageFile");
            if (FileUpload1.PostedFile.ContentLength > 0)
            {
                if (File.Exists(@"C:\Users\WJM\documents\visual studio 2013\Projects\DEMO\DEMO\ServerImages\" + FileUpload1.PostedFile.FileName))
                {
                    Label1.Text = "文件已經存在";
                }
                else
                {
                    FileUpload1.PostedFile.SaveAs(@"C:\Users\WJM\documents\visual studio 2013\Projects\DEMO\DEMO\ServerImages\" + FileUpload1.PostedFile.FileName);

                    this.Image1.ImageUrl = this.Image1.ImageUrl = @"ServerImages/"+FileUpload1.PostedFile.FileName;//相對路徑,將上傳的圖片給Image控件;//相對路徑,將上傳的圖片給Image控件
                    Label1.Text = "上傳成功!";
                }

            }
            else
            {
                Label1.Text = "上傳失敗";
            }

  四、配置文件

<?xml version="1.0" encoding="utf-8"?>
<!--
  有關如何配置 ASP.NET 應用程序的詳細信息,請訪問
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <httpRuntime executionTimeout="36000" delayNotificationTimeout="36000" maxRequestLength="2147483647" targetFramework="4.5"></httpRuntime>
     <compilation debug="true" targetFramework="4.5" />
    <!--<httpRuntime targetFramework="4.5" />-->
  </system.web>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648"/>
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

  註釋:對於配置文件不太熟悉的朋友,能夠參照個人另外一篇博客ASP.NET Web.config

   Web控件FileUpload實現

      一、上傳界面

3、文件大小屬於[1G,10G]範圍

 

註釋:未完,敬請期待。。。。。。。

  • 感謝您的閱讀,如有不足之處,歡迎指教,共同窗習、共同進步。
  • 博主網址:http://www.cnblogs.com/wangjiming/。
  • 本博客爲博主原創
  • 如您喜歡,麻煩推薦一下;如您有新想法,歡迎提出,郵箱:2016177728@qq.com。
  • 能夠轉載該博客,但必須著名博客來源。
相關文章
相關標籤/搜索