欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

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

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

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

1、頁(yè)面需要引用這個(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、頁(yè)面添加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 的摘要說(shuō)明
 /// </summary>
 public class Upload : IHttpHandler {

  public void ProcessRequest(HttpContext context) {
   context.Response.ContentType = "text/plain";
   //context.Response.Write("Hello World");

   string r = "";
   //此處有時(shí)候穿過(guò)來(lái)的sn后面還有一些亂七八糟的字符,沒(méi)研究什么意思,就判斷一下,截取一下就完事了,小項(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;
   }
  }
 }
}

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

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

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

上一篇:C#使用LINQ中Enumerable類方法的延遲與立即執(zhí)行的控制

欄    目:C#教程

下一篇:C#實(shí)現(xiàn)從多列的DataTable里取需要的幾列

本文標(biāo)題:C#簡(jiǎn)單實(shí)現(xiàn)文件上傳功能

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6633.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有