[程式碼範例]用Fine Uploader+ASP.NET MVC實現ajax檔案上傳上傳檔案就這麼簡單:Request.InputStream to FileStream

來源:互聯網
上載者:User

Fine Uploader(http://fineuploader.com/)是一個實現 ajax 上傳檔案的 Javascript 組件。

This project attempts to achieve a user-friendly file-uploading experience over the web. It's built as a Javascript plugin for developers looking to incorporate file-uploading into their website.

Fine Uploader 不依賴於 jQuery,也就是說不引用jquery.js,也可以正常使用。同時,它也提供了 jQuery Wrapper,可以方便地與jQuery整合。

這篇博文中的範例程式碼用的就是 Fine Uploader jQuery Wrapper。下面看範例程式碼:

Web前端實現

1. 下載jQuery Plug-in Fine Uploader,:https://github.com/valums/file-uploader/wiki/Releases

2. html代碼:

<!DOCTYPE html><html><head>    <meta charset="utf-8" />    <title>圖片上傳 - 部落格園</title>    <link href="/css/fineuploader.css" rel="stylesheet">        <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>    <script src="/scripts/jquery.fineuploader-3.0.min.js"></script>    </head><body>    <div id="jquery-wrapped-fine-uploader"></div>    <script>        $(function () {            $('#jquery-wrapped-fine-uploader').fineUploader({                request: {                    endpoint: '/ImageUploader/ProcessUpload'                }            });        });    </script></body></html>

代碼說明:

a) <div id="jquery-wrapped-fine-uploader"></div>用於顯示上傳按鈕

b) endpoint 設定的是上傳時服務端處理ajax請求的網址。

3. 瀏覽器中的顯示效果

伺服器 ASP.NET MVC 實現代碼

Fine Uploader 的原始碼中用 VB.NET 實現了一個 Controller(UploadController.vb),我們在使用時改為了 C# 代碼:

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web;using System.Web.Mvc;namespace CNBlogs.Upload.Web.Controllers{    public class ImageUploaderController : Controller    {        const int ChunkSize = 1024 * 1024;        public ActionResult Upload()        {            return View();        }        public ActionResult ProcessUpload(string qqfile)        {            using (var stream = Request.InputStream)            {                using (var br = new BinaryReader(stream))                {                    WriteStream(br, qqfile);                }            }            return Json(new { success = true });        }        private void WriteStream(BinaryReader br, string fileName)        {            byte[] fileContents = new byte[] { };            var buffer = new byte[ChunkSize];            while (br.BaseStream.Position < br.BaseStream.Length - 1)            {                if (br.Read(buffer, 0, ChunkSize) > 0)                {                    fileContents = fileContents.Concat(buffer).ToArray();                }            }            using (var fs = new FileStream(@"C:\\temp\\" + DateTime.Now.ToString("yyyyMMddHHmmSS") +                 Path.GetExtension(fileName).ToLower(), FileMode.Create))            {                using (var bw = new BinaryWriter(fs))                {                    bw.Write(fileContents);                }            }        }    }}

伺服器端實現改進版

public ActionResult ProcessUpload(string qqfile){    using (var inputStream = Request.InputStream)    {        using (var flieStream = new FileStream(@"c:\temp\" + qqfile, FileMode.Create))        {            inputStream.CopyTo(flieStream);        }    }    return Json(new { success = true });}

相關博文:上傳檔案就這麼簡單:Request.InputStream to FileStream

圖片上傳結果示範

 

相關文章

聯繫我們

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