zoukankan      html  css  js  c++  java
  • C# 文件上传

     一、分析

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

      3、后端代码  

     //string serverpath = Server.MapPath("~/ImageFile");
                if (FileUpload1.PostedFile.ContentLength > 0)
                {
                    if (File.Exists(@"C:UsersWJMdocumentsvisual studio 2013ProjectsDEMODEMOServerImages" + FileUpload1.PostedFile.FileName))
                    {
                        Label1.Text = "文件已经存在";
                    }
                    else
                    {
                        FileUpload1.PostedFile.SaveAs(@"C:UsersWJMdocumentsvisual studio 2013ProjectsDEMODEMOServerImages" + FileUpload1.PostedFile.FileName);

                        this.Image1.ImageUrl = this.Image1.ImageUrl = @"ServerImages/"+FileUpload1.PostedFile.FileName;//相对路径,将上传的图片给Image控件;//相对路径,将上传的图片给Image控件
                        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/。
    • 本博客为博主原创
    • 如您喜欢,麻烦推荐一下;如您有新想法,欢迎提出,邮箱:2016177728@qq.com。
    • 可以转载该博客,但必须著名博客来源。
  • 相关阅读:
    考研级《计算机网络》知识梳理——第五期
    考研级《计算机网络》知识梳理——第三期
    考研级《计算机网络》知识梳理——第九期
    考研级《计算机网络》知识梳理——第八期
    考研级《计算机网络》知识梳理——第四期
    考研级《计算机网络》知识梳理——第十期
    如果你起走得更远!
    vim列编辑模式!
    CSS深入之label与input对齐!
    js中array的sort()方法!
  • 原文地址:https://www.cnblogs.com/wangjiming/p/6267387.html
Copyright © 2011-2022 走看看