SWFUpload asp.net 返回上传文件的新文件名
我正在尝试实现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方案:
如果我查看 handlers.js (由 swfupload 提供)内部,我可以捕获从 upload.aspx.cs 返回的内容
在 upload.aspx.cs 内部写入:
在 handler.js (从演示页面下载)内部将 uploadSuccess 替换为:
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:
Inside handler.js (downloaded from demo page) replace uploadSuccess with: