C#⽂件上传(可以多⽂件上传)
⼀、前端搭建
2、根据你的需要在你项⽬适当的位置建⽴上传⽂件的⽬录列如(File)
到此前端搭建结束
⼆、配置⽂件修改(可选择跳过此步骤)
1、⾸先说明下,这个步骤可以跳过,此步骤主要是修改上传⽂件⼤⼩的限制( 默认最⼤只能上传4M)如若需要修改请继续阅读该步骤。
2、打开fig 配置⽂件到<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;)
后端代码如下
1///<summary>
2///⽂件上传页⾯
3///</summary>
4///<returns></returns>
5public ActionResult FileUpdateView()
6 {
7return View();
8 }
9
10///<summary>
11///⽂件处理⽅法
12///</summary>
13///<param name="filedata"></param>
14///<returns></returns>
15public ActionResult UploadifyFile(HttpPostedFileBase filedata)
16 {
17if (filedata == null ||
18 String.IsNullOrEmpty(filedata.FileName) ||
19 filedata.ContentLength == 0)
20 {
21return HttpNotFound();
22 }
23
24string filename = System.IO.Path.GetFileName(filedata.FileName);
25string virtualPath = String.Format("~/File/{0}", filename);
26
27string path = Server.MapPath(virtualPath);
28// 以下注释的代码都可以获得⽂件属性
29// System.Diagnostics.FileVersionInfo info = System.Diagnostics.FileVersionInfo.GetVersionInfo(path); 30// FileInfo file = new FileInfo(filedata.FileName);
31
32 filedata.SaveAs(path);
33return null;
34 }
View Code
注: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="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/lib/uploadify/jquery.uploadify.min.js"></script>
注:这⾥我们⽤到了jquery
4、前端代码
1 <script type="text/javascript">
2var uploadifyOnSelectError;
3var uploadifyOnUploadError;
4var uploadifyOnSelect;
5var uploadifyOnUploadSuccess;
6 uploadifyOnSelectError = function (file, errorCode, errorMsg) {
7var msgText = "上传失败\n";
8switch (errorCode) {
9case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
10//Msg = "每次最多上传 " + this.settings.queueSizeLimit + "个⽂件";
11 msgText += "每次最多上传 " + this.settings.queueSizeLimit + "个⽂件";
前端大文件上传解决方案
12break;
13case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
14 msgText += "⽂件⼤⼩超过限制( " + this.settings.fileSizeLimit + " )";
15break;
16case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
17 msgText += "⽂件⼤⼩为0";
18break;
19case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
20 msgText += "⽂件格式不正确,仅限 " + this.settings.fileTypeExts;
21break;
22default:
23 msgText += "错误代码:" + errorCode + "\n" + errorMsg;
24 }
25 layer.msg(msgText);
26 };
27 uploadifyOnUploadError = function (file, errorCode, errorMsg, errorString) {
28// ⼿⼯取消不弹出提⽰
29if (errorCode == SWFUpload.UPLOAD_ERROR.FILE_CANCELLED
30 || errorCode == SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED) {
31return;
32 }
33var msgText = "上传失败\n";
34switch (errorCode) {
35case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
36 msgText += "HTTP 错误\n" + errorMsg;
37break;
38case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
39 msgText += "上传⽂件丢失,请重新上传";
40break;
41case SWFUpload.UPLOAD_ERROR.IO_ERROR:
42 msgText += "IO错误";
43break;
44case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
45 msgText += "安全性错误\n" + errorMsg;
46break;
47case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
48 msgText += "每次最多上传 " + this.settings.uploadLimit + "个";
49break;
50case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
51 msgText += errorMsg;
52break;
53case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
54 msgText += "不到指定⽂件,请重新操作";
55break;
56case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
57 msgText += "参数错误";
58break;
59default:
60 msgText += "⽂件:" + file.name + "\n错误码:" + errorCode + "\n"
61 + errorMsg + "\n" + errorString;
62 }
63 layer.msg(msgText);
64 };
65
66 uploadifyOnSelect = function () {
67 };
68 uploadifyOnUploadSuccess = function (file, data, response) {
69 layer.msg(file.name + "\n\n" + response + "\n\n" + data);
70 };
71 $(function () {
72
73 $("#uploadify").uploadify({
74 uploader: '/PageBase/UploadifyFun', //处理上传的⽅法
75 swf: '/Scripts/lib/uploadify/uploadify.swf',
76 width: 80, // 按钮宽度
77 height: 60, //按钮⾼度
78 buttonText: "上传⽂件",
79 buttonCursor: 'hand',
80 fileSizeLimit:20480,
81 fileobjName: 'Filedata',
82 fileTypeExts: '*.xlsx;*.docx', //扩展名
83 fileTypeDesc: "请选择xslx,docx⽂件", //⽂件说明
84 auto: false, //是否⾃动上传
85 multi: true, //是否⼀次可以选中多个⽂件
86 queueSizeLimit: 5, //允许同时上传⽂件的个数
87 overrideEvents: ['onSelectError', 'onDialogClose'], // 是否要默认提⽰要就不配置
88 onSelect: uploadifyOnSelect,
89 onSelectError: uploadifyOnSelectError,
90 onUploadError: uploadifyOnUploadError,
91 onUploadSuccess: uploadifyOnUploadSuccess
92 });
93 });
94 </script>
95 <span id="uploadify"></span>
96 <div>
97 <a href="javascript:$('#uploadify').uploadify('upload','*');">上传</a>
98 <a href="javascript:$('#uploadify').uploadify('cancel', '*');">取消</a>
99 </div>
View Code
注:fileSizeLimit 属性的值最好和我们fig ⾥设置的⽂件上传最⼤值⼀样(不能⼤于这个值) 到这⾥。我们⽂件上传就结束了。
喜欢我的⽂章就关注我吧,有疑问可以留⾔。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论