SWFUpload asp.net返回上传文件的新文件名

时间:2010-08-25 01:01:14

标签: c#-4.0 asp.net-4.0 swfupload

我正在尝试实施SWFUpload以在我的网页上上传图片。文件路径存储在具有唯一ID作为键的表中,并且文件以新文件名存储在服务器上。我需要返回id或文件名,以便在以后需要时可以访问该信息。这可能吗?它是如何完成的?也许我可以更新一个asp:标签?

我有以下代码:

upload.aspx.cs (我还没有实现数据库保存,因为我想先知道这是否可行)

        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>

感谢。

1 个答案:

答案 0 :(得分:1)

我找到了解决方案: 如果我查看handlers.js(由swfupload提供)我可以捕获从upload.aspx.cs返回的内容

在upload.aspx.cs里面写:

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();
        }
    }
 }

在handler.js内部(从演示页面下载)将uploadSuccess替换为:

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);
}
}