Created
February 26, 2012 20:19
-
-
Save Aaronontheweb/1918806 to your computer and use it in GitHub Desktop.
HttpPostFileBase in MVC3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Authorize] | |
[HttpPost] | |
public void ScreenshotsAsync(string appName, HttpPostedFileBase files) | |
{ | |
AsyncManager.OutstandingOperations.Increment(); | |
var fileModel = new UploadedFileModel(); | |
var result = UploadedFileResult.NoFile; | |
var app = _appService.GetAppByWebFriendlyName(appName); | |
try | |
{ | |
if (AppService.IsAppInvalid(app)) | |
{ | |
result = UploadedFileResult.NoApp; | |
} | |
else if (UserDoesntHavePermission(app)) | |
{ | |
result = UploadedFileResult.NoPermissions; | |
} | |
else | |
{ | |
if (files == null || files.ContentLength == 0) | |
{ | |
Trace.TraceWarning("Attempt to save screenshot contained no file."); | |
result = UploadedFileResult.NoFile; | |
} | |
else | |
{ | |
//Valid the contents of the image... | |
var isFileValid = FileUploadValidator.FileIsWebFriendlyImage(files.InputStream, ByteCalculator.Megabytes(5)); | |
if (isFileValid) | |
{ | |
var fileName = files.FileName; | |
var imageId = Guid.NewGuid().ToString(); | |
var thumbnail = ThumbnailHelper.GetResizedImage(files.InputStream); | |
var thumbnailUri = _storageService.UploadImage(User.Identity.Name, | |
string.Format("thumb-{0}-{1}", imageId, fileName), | |
files.ContentType, thumbnail); | |
var fullImageUri = _storageService.UploadImage(User.Identity.Name, | |
string.Format("{0}-{1}", imageId, fileName), | |
files.ContentType, files.InputStream); | |
var content = | |
XfstAppContent.Create(app.Owners.First(x => x.UserName.Equals(User.Identity.Name)), | |
AppContentType.Photo); | |
content.AppName = app.AppName; | |
content.ContentTitle = files.FileName; | |
content.ContentUrl = fullImageUri; | |
content.ThumbnailUrl = thumbnailUri; | |
var contentAddResult = _appService.AddAppContent(content); | |
if (contentAddResult.GetType() == typeof(MissingXfstAppContent)) | |
{ | |
result = UploadedFileResult.Failure; | |
} | |
else | |
{ | |
fileModel = new UploadedFileModel | |
{ | |
delete_type = "DELETE", | |
delete_url = | |
GetDeleteUrl( | |
System.Web.HttpContext.Current, | |
contentAddResult.ContentId), | |
name = fileName, | |
size = files.ContentLength, | |
thumbnail_url = thumbnailUri, | |
url = fullImageUri | |
}; | |
result = UploadedFileResult.Success; | |
} | |
} | |
else | |
{ | |
result = UploadedFileResult.InvalidFileType; | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
Trace.TraceError("Error trying to save file to storage. Exception: {0}, Trace: {1}", ex.Message, ex.StackTrace); | |
result = UploadedFileResult.Failure; | |
} | |
AsyncManager.Sync(() => | |
{ | |
AsyncManager.Parameters["file"] = fileModel; | |
AsyncManager.Parameters["result"] = result; | |
AsyncManager.OutstandingOperations.Decrement(); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="fileupload"> | |
@using (Html.BeginForm("Screenshots", "Uploads", new { appName = Model.WebFriendlyName }, FormMethod.Post, new { enctype = "multipart/form-data", id = "file_upload" })) | |
{ | |
<div class="fileupload-buttonbar"> | |
<input type="hidden" name="protocol" value="http" /> | |
<label class="fileinput-button"> | |
<span>Add files...</span> | |
<input type="file" name="files" id="filetargets" multiple /> | |
</label> | |
<button type="submit" class="start"> | |
Start upload</button> | |
<button type="reset" class="cancel"> | |
Cancel upload</button> | |
<button type="button" class="delete"> | |
Delete files</button> | |
</div> | |
} | |
<div class="fileupload-content"> | |
<table class="files"> | |
</table> | |
<div class="fileupload-progressbar"> | |
</div> | |
</div> | |
</div> | |
<script src="@Url.Content("~/Scripts/jquery.fileupload.js")" type="text/javascript" language="javascript"></script> | |
<script src="@Url.Content("~/Scripts/jquery.fileupload-ui.js")" type="text/javascript" language="javascript"></script> | |
<script src="@Url.Content("~/Scripts/jquery.iframe-transport.js")" type="text/javascript" language="javascript"></script> | |
<script type="text/javascript" language="javascript"> | |
/*global $ */ | |
$(function () { | |
// Initialize the jQuery File Upload widget: | |
$('#fileupload').fileupload({ | |
url: '@Url.Action("Screenshots", "Uploads", new { appName = Model.WebFriendlyName })', | |
type: 'POST', | |
dataType: 'json', | |
fileInput: $('#filetargets'), | |
uploadTemplate: $('#template-upload'), | |
downloadTemplate: $('#template-download'), | |
acceptFileTypes: /\.(gif|jpe?g|png)$/i, | |
maxFileSize: 5000000, | |
maxNumberOfFiles: 20, | |
autoUpload: false | |
}); | |
// Open download dialogs via iframes, | |
// to prevent aborting current uploads: | |
$('#fileupload .files a:not([target^=_blank])').live('click', function (e) { | |
e.preventDefault(); | |
$('<iframe style="display:none;"></iframe>') | |
.prop('src', this.href) | |
.appendTo('body'); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment