当前位置: 移动技术网 > IT编程>开发语言>c# > C#简单实现文件上传功能

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
最近项目上的一个上传文件功能,项目是mvc+ef+ligerui 来做的,贴出来大家一起分享下 1、页面需要引用这个js 和 css <script type="

最近项目上的一个上传文件功能,项目是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;
   }
  }
 }
}

页面前台处理如下图:

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

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网