当前位置: 移动技术网 > IT编程>开发语言>c# > C#文件上传的简单实现

C#文件上传的简单实现

2019年07月18日  | 移动技术网IT编程  | 我要评论

 一、分析

      本次博客,主要解决文件上传等一系列问题,将从两方面来论述,即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]范围

注释:未完,敬请期待。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网