Follow up to SwfUpload - ASP.NET Flash uploader
SwfUpload works by placing a simple 1x1px flash application onto the page, which is highly degradable. The C# example on the main site only works with single uploads at the time of writing. So below is a quick adaption of to get the multiple large file uploader working.
www.swfupload.com
http://swfupload.org/project/asp_net
Because we use a JavaScript detection routine to select
Default.aspx - JavaScript
<script src="jquery-1.2.3.pack.js" type="text/javascript"></script>
<link href="/swfupload/css/custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/swfupload/swfupload.js"></script>
<script type="text/javascript" src="/swfupload/js/swfupload.graceful_degradation.js"></script>
<script type="text/javascript" src="/swfupload/js/swfupload.queue.js"></script>
<script type="text/javascript" src="/swfupload/js/fileprogress.js"></script>
<script type="text/javascript" src="/swfupload/js/handlers.js"></script>
<script type="text/javascript">
var upload1
window.onload = function() {
upload1 = new SWFUpload({
// Backend Settings
upload_url: "../../upload.aspx",// Relative to the SWF file
post_params : {
"ASPSESSID" : "<%=Session.SessionID %>"
},
// File Upload Settings
file_size_limit : "102400",// 100MB
file_types : "*.*",
file_types_description : "All Files",
file_upload_limit : "0",
file_queue_limit : "0",
// Event Handler Settings (all my handlers are in the Handler.js file)
file_dialog_start_handler : fileDialogStart,
file_queued_handler : fileQueued,
file_queue_error_handler : fileQueueError,
file_dialog_complete_handler : fileDialogComplete,
upload_start_handler : uploadStart,
upload_progress_handler : uploadProgress,
upload_error_handler : uploadError,
upload_success_handler : uploadSuccess,
upload_complete_handler : uploadComplete,
// Flash Settings
flash_url : "/assets/swfupload/swfupload_f8.swf",// Relative to this file (or you can use absolute paths)
swfupload_element_id : "flashUI1",// Setting from graceful degradation plugin
degraded_element_id : "degradedUI1",// Setting from graceful degradation plugin
custom_settings : {
progressTarget : "fsUploadProgress1",
cancelButtonId : "btnCancel1"
},
// Debug Settings
debug: false
});
}
</script>
Default.aspx - body
<div id="uploader" style="display:none">
<div id="flashUI1" style="display: none;">
<fieldset class="flash" id="fsUploadProgress1">
<legend>File Upload</legend>
</fieldset>
<div>
<input type="button" value="Upload file (Max 100 MB)" onclick="upload1.selectFiles()" style="font-size: 8pt;" />
<input id="btnCancel1" type="button" value="Cancel Uploads" onclick="cancelQueue(upload1);" disabled="disabled" style="font-size: 8pt;" />
<br />
</div>
</div>
<div id="degradedUI1">
Flash 8 or greater is required.
Note: can place standard asp:FileUploader here.
</div>
</div>
Uploader.aspx
public HttpPostedFile posted_file;
private static string RootDirectory = HttpRuntime.AppDomainAppPath.ToString();
public string folder = "/images/";
public string filename;
protected void Page_Load(object sender, EventArgs e)
{
try
{
posted_file = Request.Files["Filedata"];
string destination_folder = RootDirectory + folder;
posted_file.SaveAs(destination_folder + unique_filename(posted_file.FileName, destination_folder));
Response.StatusCode = 200;
}
catch
{
// If any kind of error occurs return a 500 Internal Server error
Response.StatusCode = 500;
Response.Write("An error occured");
Response.End();
}
finally
{
// Clean up
}
}
//Unique filename GetRandomFileName function
public string unique_filename(string filename, string folder)
{
filename = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) +
Path.GetExtension(orginal_file_name).ToLower();
Boolean exists = true;
//check if exists
if (File.Exists(folder + filename))
{
while (exists)
{
if (File.Exists(folder + filename))
{
exists = false;
}
else
{
filename = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) +
Path.GetExtension(orginal_file_name).ToLower();
}
}
}
return filename;
}
Uploader will return custom 500 message if required straight back to the main page.