本次博客,主要解決文件上傳等一系列問題,將從兩方面來論述,即1G以內文件和1G以上文件。
對於上傳1G以內的文件,可以采用基本的三種上傳方法:用Web控件FileUpload、html控件HtmlInputFile和用Html元素<input type="file" id="file"/>,通過Request.Files上傳。
對於1G以上的大文件,思路為:
(1)協議:可采用http協議或ftp協議
(2)斷點續傳
(3)使用插件
(4)非插件形式實現
二、文件大小屬於[0,1G]范圍html控件HtmlInputFile實現上傳:
1、上傳界面

2、前端代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="myInputFile.aspx.cs" Inherits="FileUpLoad.WebForm2" %>
<!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>
<input type="file" id="file1" runat="server" />
<asp:Button ID="btnUpLoad" runat="server" Text="上傳" OnClick="btnUpLoad_Click" />
<asp:Label ID="Label1" runat="server" Text="" ></asp:Label>
</div>
</form>
</body>
</html>
3、後端代碼
protected void btnUpLoad_Click(object sender, EventArgs e)
{
//string serverpath = Server.MapPath("~/ImageFile");
if (file1.PostedFile.ContentLength > 0)
{
if (File.Exists(@"C:\Users\WJM\Desktop\FilesUpLoad\" + file1.PostedFile.FileName))
{
Label1.Text = "文件已經存在";
}
else
{
file1.PostedFile.SaveAs(@"C:\Users\WJM\Desktop\FilesUpLoad\" + file1.PostedFile.FileName);
Label1.Text = "上傳成功!";
}
}
else
{
Label1.Text = "上傳失敗";
}
}
4、配置文件
<?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實現
1、上傳界面
三、文件大小屬於[1G,10G]范圍
注釋:未完,敬請期待。。。。。。。