Implementing Ajax file uploads with fine uploader+asp.net MVC [code example]_ practical Tips

Source: Internet
Author: User
Tags wrapper
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 into file-uploading their.

Fine Uploader does not rely on jQuery, which means it does not reference jquery.js, and can be used normally. It also provides a jquery wrapper that can be easily integrated with jquery.
The example code in this blog post is the Fine Uploader jQuery wrapper. Look at the sample code below:

Web Front-End Implementation

1. Download jquery Plug-in Fine Uploader, download address: https://github.com/valums/file-uploader/wiki/Releases
Cloud Habitat Community Fine uploader download Address http://www.jb51.net/codes/70040.html
2. HTML code:
Copy Code code as follows:

<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title> Photo Upload-Blog Park </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>
<body>
<div id= "Jquery-wrapped-fine-uploader" ></div>
<script>
$ (function () {
$ (' #jquery-wrapped-fine-uploader '). Fineuploader ({
Request: {
Endpoint: '/imageuploader/processupload '
}
});
});
</script>
</body>

Code Description:
A) <div id= "Jquery-wrapped-fine-uploader" ></div> for displaying the upload button
B endpoint set the URL that the server handles the AJAX request when uploading.
3. The display effect in the browser


Server asp.net MVC Implementation code
Fine Uploader's source code uses vb.net to implement a Controller (Uploadcontroller.vb), which we changed to C # code when we used it:
Copy Code code 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 implementation of improved version
Copy Code code 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});
}

Picture Upload Results Demo

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.