Fine Uploader (http://fineuploader.com/) is a Javascript component that implements ajax file upload.
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 in‑ate file-uploading into their website.
Fine Uploader does not depend on jQuery, that is, it can be used normally without referencing jquery. js. It also provides jQuery Wrapper, which can be easily integrated with jQuery.
The sample code in this blog post uses Fine Uploader jQuery Wrapper. The sample code is as follows:
Web Front-end implementation
1. Download jQuery Plug-in Fine Uploader,: https://github.com/valums/file-uploader/wiki/Releases
2. html code:
<! DOCTYPE html>
Code Description:
A) <div id = "jquery-wrapped-fine-uploader"> </div> used to display the upload button
B) the endpoint is the URL at which the server processes the ajax request during the upload.
3. Display Effect in the browser
Server ASP. net mvc implementation code
In the Fine Uploader source code, a Controller (UploadController. VB) is implemented using vb. NET. We changed it to C # code during use:
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); } } } }}
Server-side Ultimate Edition
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 });}
Related blog posts: uploading files is so simple: Request. InputStream to FileStream
Image Upload Result demonstration