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

作者:lijiao 时间:2022-10-03 17:29:41 

最近项目上的一个上传文件功能,项目是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、代码如下:


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#,文件上传
0
投稿

猜你喜欢

  • 解决@RequestBody搭配@Data的大坑

    2022-08-29 18:25:35
  • mybatis foreach 循环 list(map)实例

    2023-11-23 23:39:05
  • C#中IEnumerable接口用法实例分析

    2022-03-24 12:40:21
  • 如何解决修改StaticText的字体

    2023-05-25 08:40:14
  • Java Idea高效率配置技巧实例解析

    2023-06-05 13:24:42
  • Springboot启动执行特定代码的方式汇总

    2023-08-06 04:42:24
  • mongo分布式锁Java实现方法(推荐)

    2023-06-30 21:28:43
  • 开源一个c# 新的雪花算法

    2022-01-20 16:08:56
  • Java图形用户界面设计(Swing)的介绍

    2022-08-23 03:29:37
  • C语言编程C++动态内存分配示例讲解

    2023-11-02 18:00:12
  • java中map与实体类的相互转换操作

    2023-03-14 01:42:35
  • Android使用Gallery实现照片拖动的特效

    2022-12-31 23:04:22
  • Spring mvc拦截器实现原理解析

    2023-06-06 15:26:33
  • Android WebView基础应用详解

    2023-09-30 07:36:46
  • IDEA 2021.3 使用及idea2021.3.1激活使用方法

    2021-06-06 03:49:38
  • 如何让java只根据数据库表名自动生成实体类

    2022-02-24 04:25:52
  • java秒杀之redis限流操作详解

    2022-07-08 09:26:57
  • SpringBoot打成war包在tomcat或wildfly下运行的方法

    2023-11-23 08:20:56
  • Mybatis中 mapper-locations和@MapperScan的作用

    2023-07-13 08:08:34
  • java简单解析xls文件的方法示例【读取和写入】

    2022-04-15 19:30:43
  • asp之家 软件编程 m.aspxhome.com