Uploadify在asp.net下使用Demo

來源:互聯網
上載者:User

標籤:

為了使自己以後不再去網上搜尋,特記錄下來

 

從uploadify官網http://www.uploadify.com/上下載檔案

必要的檔案:

1、jquery的js檔案

2、jquery.uploadify.min.js

3、uploadify.css

4、uploadify.swf

頁面的完整代碼:

<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title>    <script src="scripts/jquery.min.js" type="text/javascript"></script>    <script src="scripts/uploadify/jquery.uploadify.min.js" type="text/javascript"></script>    <link rel="stylesheet" type="text/css" href="scripts/uploadify/uploadify.css">    <style type="text/css">    body {    font: 13px Arial, Helvetica, Sans-serif;    }    .uploadify-button {            background-color: transparent;            border: none;            padding: 0;        }    .uploadify:hover .uploadify-button {        background-color: transparent;    }    </style></head><body><h1>Uploadify在asp.net下使用Demo</h1><form><div id="queue"></div><input id="file_upload" name="file_upload" type="file" multiple="true"></form><script type="text/javascript">$(function() {    $(‘#file_upload‘).uploadify({        ‘buttonText‘: ‘選擇檔案‘,        ‘buttonImage‘: ‘scripts/uploadify/browse-btn.png‘,  //按鈕使用圖片        //‘buttonClass‘:‘custom-class‘,                //‘height‘:20,                //‘width‘:20,        //‘formData‘: { ‘submitType‘: ‘image‘, ‘studentId‘: studentId, ‘taskId‘: taskId },        ‘removeTimeout‘: 1, //進度條消失秒數        ‘fileTypeDesc‘: ‘圖片檔案‘,        ‘fileTypeExts‘: ‘*.jpg; *.jpeg; *.png; *.gif,*.bmp‘,                ‘onUploadSuccess‘: function (file, data, response) { //上傳成功回調方法                    data = eval("("+data+")");                    if (data.error == 0) {                        alert("上傳成功" + data.filename);                    } else {                        alert(data.message);                        return;                    }                },‘swf‘      : ‘scripts/uploadify/uploadify.swf‘,‘uploader‘: ‘scripts/uploadify/UploadHandler.ashx‘});});    </script></body></html>

  我的網站目錄結構:

UploadHandler.ashx是處理上傳的檔案的一般處理常式:

public class UploadHandler : IHttpHandler    {        private HttpContext context;        public void ProcessRequest(HttpContext context)        {            //檔案儲存路徑            String savePath = context.Server.MapPath("~/UploadFiles/");            if (!Directory.Exists(savePath))   //如果這個路徑不存在,則先建立            {                Directory.CreateDirectory(savePath);            }            //定義允許上傳的副檔名            Hashtable extTable = new Hashtable();            extTable.Add("image", "gif,jpg,jpeg,png,bmp");                        //最大檔案大小            int maxSize = 1000000;            this.context = context;            HttpPostedFile file = context.Request.Files["Filedata"];            if (file == null)            {                showError("請選擇檔案。");            }            if (file.InputStream == null || file.InputStream.Length > maxSize)            {                showError("上傳檔案大小超過限制。");            }            String fileName = file.FileName;   //檔案名稱            String fileExt = Path.GetExtension(fileName).ToLower();  //副檔名 如 .jpg            if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable["image"]).Split(‘,‘), fileExt.Substring(1).ToLower()) == -1)            {                showError("上傳副檔名是不允許的副檔名。\n只允許" + ((String)extTable["image"]) + "格式。");            }            String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;            String filePath = savePath + newFileName;            file.SaveAs(filePath);            //將儲存了的新的檔案名稱返回給前端            Hashtable hash = new Hashtable();            hash["error"] = 0;            hash["filename"] = newFileName;            context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");            context.Response.Write(JsonConvert.SerializeObject(hash));            context.Response.End();        }        private void showError(string message)        {            Hashtable hash = new Hashtable();            hash["error"] = 1;            hash["message"] = message;            context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");            context.Response.Write(JsonConvert.SerializeObject(hash));            context.Response.End();        }        public bool IsReusable        {            get            {                return false;            }        }    }

  

 

源碼下載(微雲)

Uploadify在asp.net下使用Demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.