Fine Uploader(http://fineuploader.com/)是一个实现 ajax 上传文件的 Javascript 组件
 

<!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 ></div> 
<script> 
$(function () { 
$('#jquery-wrapped-fine-uploader').fineUploader({ 
request: { 
endpoint: '/ImageUploader/ProcessUpload' 

}); 
}); 
</script> 
</body> 
</html> 

代码说明: 
a) <div >
服务器 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 }); 

图片上传结果演示 

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

相关文章:

  • 2021-12-22
  • 2021-07-07
  • 2021-08-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-09
  • 2021-06-15
  • 2021-12-17
  • 2022-12-23
  • 2021-07-30
相关资源
相似解决方案