ASP.NET中如何使用SWFUpload上传文件
ASP.NET中如何使用SWFUpload上传文件来源:网络转摘 作者:佚名 浏览: 1784 次 2009-2-20 17:44:53
前陣子有人提到這個上傳工具,小弟沒玩過就給它抓下來試試
SWFUpload可以支援多檔上傳功能,還不錯用,小弟分享一下試用的結果
首先要將官網的Demo Sample抓下來,如下所示:
SWFUpload下載網址:http://swfupload.googlecode.com
SWFUpload 下載檔案:SWFUpload-Samples v2.1.0.Release.zip
在\SWFUpload Samples v2.1.0\demos\applicationdemo.net目錄裡有下列檔案
接下來只要修改Default這支程式就可以了..我 只增加了儲存選取檔案的功能..
和清除目前選取檔案的功能..更多的功能就要自己去修 改了...
asp.net(c#)
Default.aspx
view plaincopy to clipboardprint?
<%@ Page Language="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>SWFUpload Revision v2.1.0 Application Demo (ASP.Net 2.0)</title>
<linkhref="css/default.css"rel="stylesheet"type="text/css"/>
<scripttype="text/javascript"src="swfupload/swfupload.js"></script>
<scripttype="text/javascript"src="js/handlers.js"></script>
<scripttype="text/javascript">
var swfu;
window.onload = function () {
swfu = new SWFUpload({
// Backend Settings
upload_url: "upload.aspx", // Relative to the SWF file
post_params : {
"ASPSESSID" : "<%=Session.SessionID %>"
},
// File Upload Settings
file_size_limit : "2048", // 2MB
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.
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,
// Flash Settings
flash_url : "swfupload/swfupload_f9.swf", // Relative to this file
custom_settings : {
upload_target : "divFileProgressContainer"
},
// Debug Settings
debug: false
});
}
</script>
</head>
<body>
<formid="form1"runat="server">
<divid="header">
<h1id="logo"><ahref="../">SWFUpload</a></h1>
<divid="version">v2.1.0 Beta</div>
</div>
<divid="content">
<h2>Application Demo (ASP.Net 2.0)</h2>
<divid="swfu_container"style="margin: 0px 10px;">
<div>
<buttonid="btnBrowse"type="button"style="padding: 5px;"onclick="swfu.selectFiles(); this.blur();"><imgsrc="images/page_white_add.png"style="padding-right: 3px; vertical-align: bottom;"alt="Add Icon"/>Select Images <spanstyle="font-size: 7pt;">(2 MB Max)</span></button>
<asp:ButtonID="btnSave"runat="server"OnClick="btnSave_Click"Text="Save Select Images"Width="150px"/>
<asp:ButtonID="btnClear"runat="server"Text="Clear Select Images"OnClick="btnClear_Click"Width="150px"/></div>
<divid="divFileProgressContainer"style="height: 75px;"></div>
<divid="thumbnails"></div>
</div>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
// Clear the user's session
if (!IsPostBack)
{
Session.Clear();
}
}
protectedvoid btnSave_Click(object sender, EventArgs e)
{
if (Session["file_info"] != null)
{
List<Thumbnail> thumbnails = Session["file_info"] as List<Thumbnail>;
string UploadPath = Server.MapPath("upload/");
foreach (Thumbnail img in thumbnails)
{
FileStream fs = new FileStream(UploadPath + img.ID + ".jpg", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(img.Data);
bw.Close();
fs.Close();
}
Session.Clear();
}
}
protectedvoid btnClear_Click(object sender, EventArgs e)
{
Session.Clear();
}
}