欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2016年03月17日 15:30:17   投稿:lijiao  
這篇文章主要介紹了C#簡(jiǎn)單實(shí)現(xiàn)文件上傳功能,利用MVC+EF+LigerUI 實(shí)現(xiàn)的upload上傳功能,感興趣的小伙伴們可以參考一下

最近項(xiàng)目上的一個(gè)上傳文件功能,項(xiàng)目是MVC+EF+LigerUI 來做的,貼出來大家一起分享下

1、頁面需要引用這個(gè)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 = "";
   //此處有時(shí)候穿過來的sn后面還有一些亂七八糟的字符,沒研究什么意思,就判斷一下,截取一下就完事了,小項(xiàng)目~
   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不同(有的是文件絕對(duì)路徑,有的是只有文件名),故要進(jìn)行處理
    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));
    //下面這句代碼缺少的話,上傳成功后上傳隊(duì)列的顯示不會(huì)自動(dòng)消失
    //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;
   }
  }
 }
}

頁面前臺(tái)處理如下圖:

#FilesUrl 是一個(gè)文本框,將上傳文件的路徑賦值進(jìn)去,將地址存入數(shù)據(jù)庫(kù),后續(xù)直接根據(jù)地址可以下載查看。

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

相關(guān)文章

最新評(píng)論