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#使用LINQ中Enumerable類方法的延遲與立即執(zhí)行的控制
欄 目:C#教程
下一篇:C#實(shí)現(xiàn)從多列的DataTable里取需要的幾列
本文標(biāo)題:C#簡單實(shí)現(xiàn)文件上傳功能
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6633.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置


