Use Fine Uploader + ASP. net mvc to upload ajax files [Sample Code]

Source: Internet
Author: User

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
Script house Fine Uploader http://www.jb51.net/codes/70040.html
2. html code:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8"/>
<Title> upload images-blog center </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>

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:
Copy codeThe Code is as follows:
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
Copy codeThe Code is as follows:
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 });
}

Image Upload Result demonstration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.