国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#簡單實現(xiàn)文件上傳功能

2020-01-24 01:14:18
字體:
供稿:網(wǎng)友

最近項目上的一個上傳文件功能,項目是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 {    //獲取上傳的文件數(shù)據(jù)    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 是一個文本框,將上傳文件的路徑賦值進去,將地址存入數(shù)據(jù)庫,后續(xù)直接根據(jù)地址可以下載查看。

以上就是實現(xiàn)C#文件上傳功能的簡單三步,希望對大家的學(xué)習(xí)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 合江县| 将乐县| 宁夏| 衡南县| 边坝县| 竹山县| 老河口市| 行唐县| 巴中市| 洮南市| 乐山市| 柘荣县| 文山县| 满洲里市| 通化县| 商丘市| 宜城市| 新蔡县| 南阳市| 利辛县| 塘沽区| 曲周县| 溆浦县| 独山县| 民丰县| 大荔县| 扬州市| 海南省| 望江县| 濮阳县| 武胜县| 吴桥县| 盱眙县| 蕉岭县| 芜湖市| 竹溪县| 家居| 澄江县| 衡南县| 凤山市| 民权县|