in

Stuart Manning

Belfast Web Development | C# | ASP.NET | Ajax | LINQ | JQuery | CSS | Flex

ASP.NET

Large File upload. SwfUpload & C#

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.

Comments

 

Diego said:

It's possible to have a working code of your implementation please ?

November 4, 2008 6:23
 

Stuart Manning said:

Diego, thats pretty much it..

I might have pulled a few extra lines of addition business logic.. but it should be intact.. where can i help you?

November 5, 2008 2:31
 

Stuart Manning said:

<pre>

<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">

</pre>

November 5, 2008 2:34
 

Diego said:

Stuart,

It works now, this was an invaluable starting point for me. Thanks LOT !!!

;)

November 5, 2008 10:46

Leave a Comment

(required)  
(optional)
(required)  
Add

About Stuart Manning

ASP.NET 3.5 C#, CSS, Javascript and loving it