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

发布于 2024-09-15 17:04:21 字数 3971 浏览 3 评论 0原文

我正在尝试实现 SWFUpload 以在我的页面上上传图像。文件路径存储在一个以唯一 id 作为键的表中,文件以新文件名存储在服务器上。我需要返回 id 或文件名,以便我以后需要时可以访问该信息。这可能吗?是如何做到的?也许我可以更新一个 asp:label?

我有以下代码:

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>

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱已欠费 2024-09-22 17:04:21

我找到了解决方案:
如果我查看 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);
}
}

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);
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文