日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

[代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传

發布時間:2024/4/17 asp.net 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[代碼示例]用Fine Uploader+ASP.NET MVC實現ajax文件上傳

原文 [代碼示例]用Fine Uploader+ASP.NET MVC實現ajax文件上傳

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

圖片上傳結果演示

?

posted on 2014-01-13 00:40 NET未來之路 閱讀(...) 評論(...) 編輯 收藏

轉載于:https://www.cnblogs.com/lonelyxmas/p/3516781.html

總結

以上是生活随笔為你收集整理的[代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。