国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

用Fine Uploader+ASP.NET MVC實(shí)現(xiàn)ajax文件上傳[代碼示例]

2024-07-10 13:23:06
字體:
供稿:網(wǎng)友
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前端實(shí)現(xiàn)

1. 下載jQuery Plug-in Fine Uploader,下載地址:https://github.com/valums/file-uploader/wiki/Releases
腳本之家Fine Uploader下載地址
2. html代碼:

復(fù)制代碼 代碼如下:


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>圖片上傳 - 博客園</title>
<link href="/css/fineuploader.css">
<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></div>
<script>
$(function () {
$('#jquery-wrapped-fine-uploader').fineUploader({
request: {
endpoint: '/ImageUploader/ProcessUpload'
}
});
});
</script>
</body>
</html>


代碼說明:
a) <div></div>用于顯示上傳按鈕
b) endpoint 設(shè)定的是上傳時服務(wù)端處理ajax請求的網(wǎng)址。
3. 瀏覽器中的顯示效果


服務(wù)器 ASP.NET MVC 實(shí)現(xiàn)代碼
Fine Uploader 的源代碼中用 VB.NET 實(shí)現(xiàn)了一個 Controller(UploadController.vb),我們在使用時改為了 C# 代碼:

復(fù)制代碼 代碼如下:


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);
}
}
}
}
}


服務(wù)器端實(shí)現(xiàn)改進(jìn)版

復(fù)制代碼 代碼如下:


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 });
}


圖片上傳結(jié)果演示

用Fine Uploader+ASP.NET MVC實(shí)現(xiàn)ajax文件上傳[代碼示例]

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 楚雄市| 运城市| 上虞市| 汪清县| 甘南县| 民权县| 五原县| 岳池县| 唐河县| 石阡县| 新源县| 德安县| 碌曲县| 河池市| 桃江县| 右玉县| 吴旗县| 师宗县| 建湖县| 芮城县| 牡丹江市| 康乐县| 屏南县| 陇南市| 沽源县| 宁波市| 松桃| 高邑县| 务川| 九寨沟县| 乌鲁木齐市| 金溪县| 河北省| 德昌县| 运城市| 盘锦市| 邵阳市| 长宁县| 清镇市| 南昌县| 金坛市|