views:

156

answers:

1

Hi,

I'm trying to implement SWFUpload for uploading images on my page. The file-path is stored in a table with a unique id as key and the file is store on the server with a new filename. I need the id or the filename to be returned so that I can access that information when I later-on need it. Is this possible and how is it done? Maybe i could update a asp:label?

I have the following code:

upload.aspx.cs (I haven't implemented the database saving yet as I want to know if this is possible first)

        protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            // Get the data
            HttpPostedFile postedfile = Request.Files["Filedata"];

            int n = 0;
            string fullPath;

            while (true) 
            {
                fullPath = Server.MapPath("Img\\") + n.ToString() + RemoveSpecialChars(postedfile.FileName);
                if (File.Exists(fullPath))
                {
                    n++;
                }
                else
                {
                    postedfile.SaveAs(fullPath);
                    break;
                }
            }

        }
        catch
        {
            // If any kind of error occurs return a 500 Internal Server error
            Response.StatusCode = 500;
            Response.Write("An error occured");
            Response.End();
        }
        finally
        {

            Response.End();
        }
    }

default.aspx

      <!--
  Image upload
  -->
    <script type="text/javascript" src="Scripts/swfupload.js"></script>
    <script type="text/javascript" src="Scripts/handlers.js"></script>
    <script type="text/javascript">
        var swfu;
        window.onload = function () {
            swfu = new SWFUpload({
                // Backend Settings
                upload_url: "Upload.aspx",
                post_params: {
                    "ASPSESSID": "<%=Session.SessionID %>"
                },

                // File Upload Settings
                file_size_limit: "5120", //5MB
                file_types: "*.jpg",
                file_types_description: "JPG Images",
                file_upload_limit: 0,    // Zero means unlimited

                // Event Handler Settings - these functions as defined in Handlers.js
                //  The handlers are not part of SWFUpload but are part of my website and control how
                //  my website reacts to the SWFUpload events.
                swfupload_preload_handler: preLoad,
                swfupload_load_failed_handler: loadFailed,
                file_queue_error_handler: fileQueueError,
                file_dialog_complete_handler: fileDialogComplete,
                upload_progress_handler: uploadProgress,
                upload_error_handler: uploadError,
                upload_success_handler: uploadSuccess,
                upload_complete_handler: uploadComplete,

                // Button settings
                button_image_url: "Style/Images/XPButtonNoText_160x22.png",
                button_placeholder_id: "spanButtonPlaceholder",
                button_width: 160,
                button_height: 22,
                button_text: '<span class="button">Välj bilder</span>',
                button_text_style: '.button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; }',
                button_text_top_padding: 1,
                button_text_left_padding: 5,

                // Flash Settings
                flash_url: "swfupload.swf", // Relative to this file
                flash9_url: "swfupload_FP9.swf", // Relative to this file

                custom_settings: {
                    upload_target: "divFileProgressContainer"
                },

                // Debug Settings
                debug: false
            });
        }
    </script>

default.aspx

    <div id="swfu_container" style="margin: 0px 10px;">
        <div>
            <span id="spanButtonPlaceholder"></span>
        </div>
        <div id="divFileProgressContainer" style="height: 75px;"></div>
    </div>

Thanks.

A: 

I found the solution: If i look inside handlers.js (provided by swfupload) I can catch whats is returned from the upload.aspx.cs

Inside upload.aspx.cs write:

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            // Get the data
            HttpPostedFile postedfile = Request.Files["Filedata"];

            Response.Write((postedfile.FileName); //Returns filename to javascript

        }
        catch
        {
            // If any kind of error occurs return a 500 Internal Server error
            Response.StatusCode = 500;
            Response.Write("An error occured");
            Response.End();
        }
        finally
        {

            Response.End();
        }
    }
 }

Inside handler.js (downloaded from demo page) replace uploadSuccess with:

function uploadSuccess(file, serverData) {
try {

   alert(serverData); 

    addImage("thumbnail.aspx?id=" + serverData);

    var progress = new FileProgress(file, this.customSettings.upload_target);

    progress.setStatus("Thumbnail Created.");
    progress.toggleCancel(false);


} catch (ex) {
    this.debug(ex);
}
}
Mikael