C#簡單實(shí)現(xiàn)文件上傳功能
最近項(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ù)庫,后續(xù)直接根據(jù)地址可以下載查看。
以上就是實(shí)現(xiàn)C#文件上傳功能的簡單三步,希望對(duì)大家的學(xué)習(xí)有所幫助。
- C# 通用文件上傳類
- C#實(shí)現(xiàn)HTTP上傳文件的方法
- asp.net(c#)開發(fā)中的文件上傳組件uploadify的使用方法(帶進(jìn)度條)
- C# 文件上傳 默認(rèn)最大為4M的解決方法
- asp.net(C#)中上傳大文件的幾中常見應(yīng)用方法
- C#采用HttpWebRequest實(shí)現(xiàn)保持會(huì)話上傳文件到HTTP的方法
- ASP.NET(C#)實(shí)現(xiàn)一次性動(dòng)態(tài)上傳多張圖片的代碼(多個(gè)文件)
- C#實(shí)現(xiàn)Web文件上傳的兩種方法實(shí)例代碼
- C#判斷上傳文件是否是圖片以防止木馬上傳的方法
- C#文件上傳的簡單實(shí)現(xiàn)
相關(guān)文章
C#實(shí)現(xiàn)創(chuàng)建標(biāo)簽PDF文件的示例代碼
標(biāo)簽PDF文件包含描述文檔結(jié)構(gòu)和各種文檔元素順序的元數(shù)據(jù),是一種包含后端提供的可訪問標(biāo)記,管理閱讀順序和文檔內(nèi)容表示的邏輯結(jié)構(gòu)的PDF文件。本文將用C#實(shí)現(xiàn)創(chuàng)建標(biāo)簽PDF文件,需要的可以參考一下2022-08-08
Ruby創(chuàng)建數(shù)組方法總結(jié)
在本篇文章里小編給大家分享了關(guān)于Ruby創(chuàng)建數(shù)組方法的知識(shí)點(diǎn)內(nèi)容,對(duì)戲有興趣的朋友們學(xué)習(xí)下。2019-01-01
unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周
這篇文章主要介紹了unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04
c#之圓形無標(biāo)題欄橢圓窗體的實(shí)現(xiàn)詳解
本篇文章是對(duì)c#中圓形無標(biāo)題欄橢圓窗體的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

