C#实现文件上传以及多文件上传功能

C#实现文件上传以及多文件上传功能,第1张

概述一、前端搭建1、前端用到js:uploadify(下载地址:http://www.uploadify.com/download/)、layer(下载地址:http://layer.layui.com/),下载之后把它们放在你的项目里列如

一、前端搭建

1、前端用到Js:uploadify(下载地址:http://www.uploadify.com/download/)、layer (下载地址:http://layer.layui.com/),下载之后把它们放在你的项目里 列如

2、根据你的需要在你项目适当的位置建立上传文件的目录  列如(file)

 到此前端搭建结束

二、配置文件修改(可选择跳过此步骤)

1、首先说明下,这个步骤可以跳过,此步骤主要是修改上传文件大小的限制(.net 默认最大只能上传4M)如若需要修改请继续阅读该步骤。

2、打开web.config 配置文件 找到<system.web> 节点 ,在该节点下面添加如下节点

<httpRuntime targetFramework="4.5"  executionTimeout="500" maxRequestLength="409600" useFullyQualifIEdRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueuelimit="100" /><!-- maxRequestLength属性是上传文件大小的设置 值是kb大小 maxRequestLength=“1024” 为最大上传1M -->

三、代码编写

1、说明下:我用的是mvc模式 所以这里就用mvc的方式编写 (代码是不变的,开发者可以根据你们的设计模式编写)

2、建立一个控制器PageBaseController在该控制器里编写如下代码 (如果是用的aspx页面那么把fileUpdateVIEw方法删掉  ,把Uploadifyfile 方法的ActionResult改成voID  并去掉return null;) 

后端代码如下

/// <summary>    /// 文件上传页面    /// </summary>    /// <returns></returns>    public ActionResult fileUpdateVIEw()    {      return VIEw();    }    /// <summary>    /// 文件处理方法    /// </summary>    /// <param name="filedata"></param>    /// <returns></returns>    public ActionResult Uploadifyfile(httpPostedfileBase filedata)    {      if (filedata == null ||        String.IsNullOrEmpty(filedata.filename) ||        filedata.ContentLength == 0)      {        return httpNotFound();      }      string filename = System.IO.Path.Getfilename(filedata.filename);      string virtualPath = String.Format("~/file/{0}",filename);      string path = Server.MapPath(virtualPath);      // 以下注释的代码 都可以获得文件属性      // System.Diagnostics.FiLeversionInfo info = System.Diagnostics.FiLeversionInfo.GetVersionInfo(path);      // fileInfo file = new fileInfo(filedata.filename);      filedata.SaveAs(path);      return null;    } 

注:virtualPath 是我们搭建上传文件的目录

3、在视图(页面)里引用我们搭建的Js:uploadfiy 、layer 路径

 列如:      

<script src="~/Scripts/jquery-1.10.2.Js"></script><script src="~/Scripts/lib/layer/layer.Js"></script><link href="~/Scripts/lib/uploadify/uploadify.CSS" rel="external nofollow" rel="stylesheet" /><script src="~/Scripts/jquery-1.10.2.min.Js"></script><script src="~/Scripts/lib/uploadify/jquery.uploadify.min.Js"></script>

注:这里我们用到了jquery

4、前端代码

<script type="text/JavaScript">  var uploadifyOnSelectError;  var uploadifyOnUploadError;  var uploadifyOnSelect;  var uploadifyOnUploadSuccess;  uploadifyOnSelectError = function (file,errorCode,errorMsg) {    var msgText = "上传失败\n";    switch (errorCode) {      case SWFUpload.QUEUE_ERROR.QUEUE_liMIT_EXCEEDED:        //this.queueData.errorMsg = "每次最多上传 " + this.settings.queueSizelimit + "个文件";        msgText += "每次最多上传 " + this.settings.queueSizelimit + "个文件";        break;      case SWFUpload.QUEUE_ERROR.file_EXCEEDS_SIZE_liMIT:        msgText += "文件大小超过限制( " + this.settings.fileSizelimit + " )";        break;      case SWFUpload.QUEUE_ERROR.ZERO_BYTE_file:        msgText += "文件大小为0";        break;      case SWFUpload.QUEUE_ERROR.INVALID_fileTYPE:        msgText += "文件格式不正确,仅限 " + this.settings.fileTypeExts;        break;      default:        msgText += "错误代码:" + errorCode + "\n" + errorMsg;    }    layer.msg(msgText);  };  uploadifyOnUploadError = function (file,errorMsg,errorString) {    // 手工取消不d出提示    if (errorCode == SWFUpload.UPLOAD_ERROR.file_CANCELLED      || errorCode == SWFUpload.UPLOAD_ERROR.UPLOAD_StopPED) {      return;    }    var msgText = "上传失败\n";    switch (errorCode) {      case SWFUpload.UPLOAD_ERROR.http_ERROR:        msgText += "http 错误\n" + errorMsg;        break;      case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:        msgText += "上传文件丢失,请重新上传";        break;      case SWFUpload.UPLOAD_ERROR.IO_ERROR:        msgText += "IO错误";        break;      case SWFUpload.UPLOAD_ERROR.Security_ERROR:        msgText += "安全性错误\n" + errorMsg;        break;      case SWFUpload.UPLOAD_ERROR.UPLOAD_liMIT_EXCEEDED:        msgText += "每次最多上传 " + this.settings.uploadlimit + "个";        break;      case SWFUpload.UPLOAD_ERROR.UPLOAD_Failed:        msgText += errorMsg;        break;      case SWFUpload.UPLOAD_ERROR.SPECIFIED_file_ID_NOT_FOUND:        msgText += "找不到指定文件,请重新 *** 作";        break;      case SWFUpload.UPLOAD_ERROR.file_VALIDATION_Failed:        msgText += "参数错误";        break;      default:        msgText += "文件:" + file.name + "\n错误码:" + errorCode + "\n"          + errorMsg + "\n" + errorString;    }    layer.msg(msgText);  };  uploadifyOnSelect = function () {  };  uploadifyOnUploadSuccess = function (file,data,response) {    layer.msg(file.name + "\n\n" + response + "\n\n" + data);  };  $(function () {    $("#uploadify").uploadify({      uploader: '/PageBase/UploadifyFun',//处理上传的方法      swf: '/Scripts/lib/uploadify/uploadify.swf',wIDth: 80,// 按钮宽度      height: 60,//按钮高度      buttonText: "上传文件",buttonCursor: 'hand',fileSizelimit:20480,fileobjname: 'filedata',fileTypeExts: '*.xlsx;*.docx',//扩展名      fileTypeDesc: "请选择xslx,docx文件",//文件说明      auto: false,//是否自动上传      multi: true,//是否一次可以选中多个文件      queueSizelimit: 5,//允许同时上传文件的个数      overrIDeEvents: ['onSelectError','onDialogClose'],// 是否要默认提示 要就不配置      onSelect: uploadifyOnSelect,onSelectError: uploadifyOnSelectError,onUploadError: uploadifyOnUploadError,onUploadSuccess: uploadifyOnUploadSuccess    });  });</script><span ID="uploadify"></span><div>  <a href="JavaScript:$('#uploadify').uploadify('upload','*');">上传</a>  <a href="JavaScript:$('#uploadify').uploadify('cancel','*');">取消</a></div> 

注:fileSizelimit 属性的值最好和我们web.config 里设置的文件上传最大值一样(不能大于这个值)

到这里。我们文件上传就结束了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的C#实现文件上传以及多文件上传功能全部内容,希望文章能够帮你解决C#实现文件上传以及多文件上传功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1255892.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存