gpt4 book ai didi

C#简单实现文件上传功能

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章C#简单实现文件上传功能由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

最近项目上的一个上传文件功能,项目是MVC+EF+LigerUI 来做的,贴出来大家一起分享下 。

1、页面需要引用这个JS 和 CSS 。

<script type="text/javascript" src="/Content/uploadify/jquery.uploadify.min.js"></script> 。

<link href="/Content/uploadify/uploadify.css" type="text/css" rel="stylesheet" /> 。

2、页面添加Upload.ashx 。

3、代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Security;
 
namespace AL.Web {
  /// <summary>
  /// Upload 的摘要说明
  /// </summary>
  public class Upload : IHttpHandler {
 
   public void ProcessRequest(HttpContext context) {
    context.Response.ContentType = "text/plain" ;
    //context.Response.Write("Hello World");
 
    string r = "" ;
    //此处有时候穿过来的sn后面还有一些乱七八糟的字符,没研究什么意思,就判断一下,截取一下就完事了,小项目~
    string sn = context.Request.QueryString[ "sn" ];
    if (sn != null && sn.Length > 14) sn = sn.Substring(0, 14);
 
    if (context.User.Identity.IsAuthenticated == false ) {
     // 未登录用户   
    }
    try {
     //获取上传的文件数据
     HttpPostedFile file = context.Request.Files[ "Filedata" ];
     string fileName = file.FileName;
     string fileType = Path.GetExtension(fileName).ToLower();
     //由于不同浏览器取出的FileName不同(有的是文件绝对路径,有的是只有文件名),故要进行处理
     if (fileName.IndexOf( ' ' ) > -1) {
      fileName = fileName.Substring(fileName.LastIndexOf( ' ' ) + 1);
     } else if (fileName.IndexOf( '/' ) > -1) {
      fileName = fileName.Substring(fileName.LastIndexOf( '/' ) + 1);
     }
     //上传的目录
     string uploadDir = "~/Content/uploadfile/TMP/" + System.DateTime.Now.ToString( "yyyyMM" ) + "/" ;
     //上传的路径
     //生成年月文件夹及日文件夹
     if (Directory.Exists(context.Server.MapPath(uploadDir)) == false ) {
      Directory.CreateDirectory(context.Server.MapPath(uploadDir));
     }
     if (Directory.Exists(context.Server.MapPath(uploadDir + System.DateTime.Now.ToString( "dd" ) + "/" )) == false ) {
      Directory.CreateDirectory(context.Server.MapPath(uploadDir + System.DateTime.Now.ToString( "dd" ) + "/" ));
     }
 
     uploadDir = uploadDir + System.DateTime.Now.ToString( "dd" ) + "/" ;
 
     string uploadPath = uploadDir + FormsAuthentication.HashPasswordForStoringInConfigFile(fileName, "MD5" ).Substring(0, 8) + fileType;
     //保存文件
     file.SaveAs(context.Server.MapPath(uploadPath));
     //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
     //DbHelperOleDb.ExecuteSql("insert into [temp](temp_sn,temp_Content) values('" + sn + "','" + uploadPath + "')");
 
     //Response.Write("1");
     //context.Response.Write("{'IsError':false, 'Data':'" + uploadPath + "'}");
     r = "{'IsError':false, 'Data':'" + uploadPath + "'}" ;
    } catch (Exception ex) {
     //Response.Write("0");
     //throw ex;
     //context.Response.Write("{IsError: true, data:'" + ex.Message + "'}");
     r = "{'IsError':true, 'Data':'" + ex.Message + "'}" ;
    } finally {
     r = r.Replace( "'" , "\"" );
     context.Response.Write(r);
     context.Response.End();
    }
   }
   public bool IsReusable {
    get {
     return false ;
    }
   }
  }
}

页面前台处理如下图:

C#简单实现文件上传功能

#FilesUrl 是一个文本框,将上传文件的路径赋值进去,将地址存入数据库,后续直接根据地址可以下载查看.

以上就是实现C#文件上传功能的简单三步,希望对大家的学习有所帮助.

最后此篇关于C#简单实现文件上传功能的文章就讲到这里了,如果你想了解更多关于C#简单实现文件上传功能的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com