C#文件上传的简单实现
作者:Alan_beijing 时间:2021-06-02 19:06:04
一、分析
本次博客,主要解决文件上传等一系列问题,将从两方面来论述,即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、前端代码
<!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="" Style="color: Red"></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]范围
注释:未完,敬请期待。
来源:http://www.cnblogs.com/wangjiming/archive/2017/01/10/6267387.html
标签:C#,文件上传
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot集成Mybatis-plus并实现自动生成相关文件的示例代码
2023-08-15 04:12:46
Maven工程pom中如何定义jdk版本
2022-02-18 08:16:13
![](https://img.aspxhome.com/file/2023/6/74986_0s.png)
springBoot @Scheduled实现多个任务同时开始执行
2023-06-19 23:16:41
![](https://img.aspxhome.com/file/2023/7/94337_0s.png)
JAVA面试题之Forward与Redirect的区别详解
2023-11-25 02:27:15
![](https://img.aspxhome.com/file/2023/2/59912_0s.png)
struts2框架的登录制作图文教程
2022-11-24 03:15:15
![](https://img.aspxhome.com/file/2023/1/70981_0s.png)
详解java中BigDecimal精度问题
2021-08-17 10:24:59
![](https://img.aspxhome.com/file/2023/9/64039_0s.jpg)
Java发送邮箱验证码、session校验功能
2023-09-11 02:44:21
java 查找字符串所在的位置代码
2023-12-22 21:48:17
![](https://img.aspxhome.com/file/2023/0/79820_0s.jpg)
详解JAVA 内存管理
2023-01-13 04:35:51
![](https://img.aspxhome.com/file/2023/8/74748_0s.png)
java多态的向上转型的概念及实例分析
2023-06-12 08:30:35
springboot post接口接受json时,转换为对象时,属性都为null的解决
2023-06-17 15:24:23
![](https://img.aspxhome.com/file/2023/3/57313_0s.png)
java数独游戏完整版分享
2023-05-22 07:09:56
Java基于zxing生成二维码矩阵过程解析
2023-11-23 06:04:06
![](https://img.aspxhome.com/file/2023/5/59845_0s.png)
Java解决计算相邻两个数的最大差值的问题
2022-03-29 05:47:20
![](https://img.aspxhome.com/file/2023/7/94307_0s.png)
C#检查键盘大小写锁定状态的方法
2022-01-11 18:00:39
Spring Security中使用authorizeRequests遇到的问题小结
2023-10-07 04:23:45
![](https://img.aspxhome.com/file/2023/2/64952_0s.png)
Java中的同步与异步详细介绍
2023-06-23 00:42:08
IDEA启动tomcat控制台中文乱码问题的解决方法(100%有效)
2021-06-25 10:45:23
![](https://img.aspxhome.com/file/2023/9/62699_0s.jpg)
Java实现图片验证码具体代码
2021-06-30 13:16:35
![](https://img.aspxhome.com/file/2023/9/60989_0s.jpg)
如何安装java的运行环境IDEA
2022-09-20 10:42:38
![](https://img.aspxhome.com/file/2023/0/64380_0s.png)