Converting uploadify code from ASP.NET 4 to MVC 3
I have a ASP.NET 4 working uploadify code that upload both single and mutiple files. I use HttpHandl开发者_开发知识库er to handle the upload at the server side once uploadify sends them.
How should I approach this in MVC 3?
Do I need to write different actions to handle single file vs multiple file upload?
I just need the structure of the code so that I can process the posted files at server.
Here are my code in ASP.NET 4:
$(function () {
$('#file_upload').uploadify({
'uploader': '/content/uploadify/uploadify.swf',
'script': '/content/uploadify/uploadimg.ashx', <<-- httphandler here
'scriptData': { 'auth': auth, 'sid': sid, 'aid': '', 'pid': 0, 'multi': 1 },
'cancelImg': '/content/uploadify/cancel.gif',
'folder': '/content/uploadify/uploads',
'auto': false,
'multi': true,
'queueSizeLimit': 10,
'sizeLimit': 2359296,
'fileExt': '*.jpg;*.jpeg',
'fileDesc': 'Photo Files ( .jpg )',
'displayData': 'speed',
'expressInstall': '/content/uploadify/expressInstall.swf',
'removeCompleted': false,
'wmode': 'transparent',
'hideButton': true,
'height': 33,
'width': 156
, 'onSelectOnce': function (event, data) { //code omitted }
, 'onError': function (e, fid, fo, eo) { //code omitted }
, 'onComplete': function (e, q, f, r, d) { //code omitted }
, 'onAllComplete': function (e, d) {
//code omitted
}
});
});
uploadimg.ashx:
public class uploadimg : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest (HttpContext context)
{
//code omitted
}
}
All you need to do is add a single action -- modified from here.
public ActionResult UploadFiles()
{
var r = new List<ViewDataUploadFilesResult>();
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength == 0)
continue;
string savedFileName = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
Path.GetFileName(hpf.FileName));
hpf.SaveAs(savedFileName);
}
return new EmptyResult();
}
And then change your upload path with your uploadify script accordingly.
精彩评论