步步為營-70-asp.net簡單練習(檔案的上傳和下載)

來源:互聯網
上載者:User

標籤:orm   path   action   eth   ted   width   str   tor   nbsp   

大檔案的上傳一般通過FTP協議,而一般小的檔案可以通過http協議來完成

1 通過asp.net 完成圖片的上傳

1.1 建立html頁面

  注意:1 method="post" ;2 enctype="multipart/form-data"; 3 <input type="file" />  

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title></title></head><body>    <form method="post" action="FileUpload.ashx" enctype="multipart/form-data">        <input type="file" id="imgUpLoad" name="imgUpLoad" />        <input type="submit" value="提交" />    </form></body></html>
FileUpload.html

1.2 建立一般處理常式.ashx
  注意:1 建立檔案儲存路徑

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web;namespace _05_檔案的上傳與下載{    /// <summary>    /// FileUpload 的摘要說明    /// </summary>    public class FileUpload : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/html";            //01 擷取檔案            HttpPostedFile pf = context.Request.Files["imgUpLoad"];            //02 建立檔案儲存路徑            string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory+"Upload/"+pf.FileName);            //03 儲存檔案            pf.SaveAs(savePath);            //04 顯示上傳的檔案            context.Response.Write("<img src=‘Upload/"+pf.FileName+"‘/> ");        }        public bool IsReusable        {            get            {                return false;            }        }    }}
FileUpload.ashx

2 上傳檔案格式的驗證,假設規定只能上傳,gif的圖片

  我們可以在HTML通過jQuery來進行驗證,也可以在.ashx中進行驗證

2.1 修改ashx檔案

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web;namespace _05_檔案的上傳與下載{    /// <summary>    /// FileUpload 的摘要說明    /// </summary>    public class FileUpload : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/html";            //01 擷取檔案            HttpPostedFile pf = context.Request.Files["imgUpLoad"];            //01-01 擷取檔案尾碼名            string extName = pf.FileName.Substring(pf.FileName.LastIndexOf(‘.‘));            if (extName != ".gif" || extName != ".Gif")            {                context.Response.Write("請上傳.gif圖片");                return;            }            //02 建立檔案儲存路徑            string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory+"Upload/"+pf.FileName);            //03 儲存檔案            pf.SaveAs(savePath);            //04 顯示上傳的檔案            context.Response.Write("<img src=‘Upload/"+pf.FileName+"‘/> ");        }        public bool IsReusable        {            get            {                return false;            }        }    }}
ashx

2.2 引入jQuery,修改HTML頁面

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <script src="http://localhost:62225/Script/jquery-1.7.1.min.js"></script>    <title></title>    <script>        $(function () {            $("form").submit(function () {                var fname = $("#imgUpLoad").val();                var extname = fname.substring(fname.lastIndexOf(‘.‘));                if (extname != ".gif" || extname != ".Gif") {                    alert("請上傳.gif圖片");                    return false;                }                           });        });    </script></head><body>    <form method="post" action="FileUpload.ashx" enctype="multipart/form-data">        <input type="file" id="imgUpLoad" name="imgUpLoad" />        <input type="submit" value="提交" />    </form></body></html>
html

3 如果檔案只放在Upload檔案夾下,隨著時間的增長,檔案勢必會越來越多不利於尋找,可以根據日期建立相應檔案夾

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web;namespace _05_檔案的上傳與下載{    /// <summary>    /// FileUpload 的摘要說明    /// </summary>    public class FileUpload : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/html";            //01 擷取檔案            HttpPostedFile pf = context.Request.Files["imgUpLoad"];            //01-01 擷取檔案尾碼名            string extName = pf.FileName.Substring(pf.FileName.LastIndexOf(‘.‘));            if (extName != ".gif" && extName != ".GIF")            {                context.Response.Write("請上傳.gif圖片");                return;            }            //02 建立檔案儲存路徑            string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory+"Upload\\");            //02-01 根據日期建立檔案夾            DateTime dt = DateTime.Now;            savePath += dt.Year + "\\" + dt.Month + "\\" + dt.Day ;            if (!Directory.Exists(savePath))            {                //建立檔案夾                Directory.CreateDirectory(savePath);            }            //02-02文 件名為目前時間                        savePath += "\\"+ dt.ToString().Replace(‘:‘,‘-‘)+".gif";            //03 儲存檔案            pf.SaveAs(savePath);            //04 顯示上傳的檔案             context.Response.Write("<img src=‘" + savePath.Substring(savePath.IndexOf("Upload")) + "‘/> ");        }        public bool IsReusable        {            get            {                return false;            }        }    }}
ashx

步步為營-70-asp.net簡單練習(檔案的上傳和下載)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.