flash + asp 上传程序
在Flash8中:
方法一:
使用file.upload可以带参数给后台的ASP程序:
file.upload(_root.iisPath+"uploadFile.asp?userName="+_root.userName+"&uploadName="+_root.uploadName);
后台的uploadFile.asp:
<!--#include FILE="upload.inc"-->
<%
userName=request("userName")
uploadName=request("uploadName")
set upload=new upload_5xSoft ''建立上传对象
set file=upload.file("Filedata") ''生成一个文件对象
if file.FileSize>0 then ''如果 FileSize > 0 说明有文件数据
file.SaveAs Server.mappath(".\"&userName&"\"&uploadName) ''保存文件
end if
set file=nothing
set upload=nothing ''删除此对象
%>
方法二:
不用file.upload方法,直接用LoadVars方法,简洁,好用.
FLA代码:
//上传代码
import flash.external.*;
browseBtn.onPress = function() {
_root.filePath = String(ExternalInterface.call("getFilePath", "Element"));
uploadTxt.text = _root.filePath;
};
uploadBtn.onPress = function() {
_root.getTime();
_root.uploadName += ".png";
var myLoadVars:LoadVars = new LoadVars();
myLoadVars.load(_root.iisPath+"uploadFile.asp?userName="+_root.userName+"&uploadName="+_root.uploadName+"&filePath="+_root.filePath);
myLoadVars.onData = function(data) {
uploadTxt.text = data;
};
};
在生成的html文件的head间插入:
<script>
function getFilePath(name) {
document.all.myForm.browFile.click()
return document.all.myForm.browFile.value;
}
</script>
在生成的html文件的body间插入:
<table width="100" border="1" id="tab" style="VISIBILITY: hidden">
<td>
<form name="myForm">
<input name="browFile" type="file" id="browFile" value="">
</form>
</td>
</table>
后台的uploadFile.asp:
<%
userName= Request("userName")
uploadName= Request("uploadName")
filePath = Request("filePath")
Set objStream = Server.createObject("ADODB.Stream")
objStream.Type = 1 '' adTypeBinary
objStream.Open
objStream.LoadFromFile filePath
objStream.SaveToFile Server.MapPath(userName&"/"&uploadName)
Response.write("上传完毕!")
%>
在Flash9中:
方法一:
使用file.upload可以带参数给后台的ASP程序:
//获取上传文件名
parent.getTime()
var uploadURL:URLRequest;
var file:FileReference;
var fileType:String
uploadURL = new URLRequest();
//uploadURL.url = "http://127.0.0.1/upLoad/uploadFile.asp?userName=hdt&uploadName=1.jpg"
trace(parent.userName)
file = new FileReference();
file.addEventListener(Event.select, selectHandler);
file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
file.addEventListener(Event.COMPLETE, completeHandler);
function selectHandler(event:Event):void {
var file:FileReference = FileReference(event.target);
//trace("selectHandler: name=" + file.name + " URL=" + uploadURL.url);
uploadTxt.text = file.name;
fileType = file.type;
}
function ioErrorHandler(event:IOErrorEvent):void {
//trace("ioErrorHandler: " + event);
uploadTxt.text ="上传失败..."
}
function progressHandler(event:ProgressEvent):void {
var file:FileReference = FileReference(event.target);
// trace("progressHandler: name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
uploadTxt.text ="正在上传..."
}
function completeHandler(event:Event):void {
//trace("completeHandler: " + event);
uploadTxt.text ="上传完毕..."
}
browseBtn.addEventListener(MouseEvent.MOUSE_DOWN,browse);
function browse(e:Event){
var imagesFilter:FileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
var docFilter:FileFilter = new FileFilter("Documents", "*.pdf;*.doc;*.txt");
file.browse([imagesFilter]);
}
uploadBtn.addEventListener(MouseEvent.MOUSE_DOWN,upload);
function upload(e:Event){
uploadURL.url = parent.iisPath+"/uploadFile.asp?userName="+parent.userName+"&uploadName="+parent.uploadName+fileType;
file.upload(uploadURL);
}
后台ASP文件同上
方法二:
不用file.upload方法,直接传文件名:
import flash.external.ExternalInterface;
var readyTimer:Timer = new Timer(100, 0);
var uploadURL:URLRequest;
browseBtn.addEventListener(MouseEvent.MOUSE_DOWN,browse);
function browse(e:Event){
if (checkJavaScriptReady()) {
filePath = String(ExternalInterface.call("getFilePath"));
uploadTxt.text=filePath
} else {
readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
readyTimer.start();
}
}
uploadBtn.addEventListener(MouseEvent.MOUSE_DOWN,upload);
function upload(e:Event){
//开始上传
var loader:URLLoader = new URLLoader();
configureListeners(loader);
parent.getTime()
var fileType:String=filePath.split(".")[1]
parent.uploadName+="."+fileType
var uploadPath:String=parent.iisPath+"/uploadFileForJs.asp?userName="+parent.userName+"&uploadName="+parent.uploadName+"&filePath="+filePath
uploadTxt.text=uploadPath
var request:URLRequest = new URLRequest(uploadPath);
try {
loader.load(request);
} catch (error:Error) {
uploadTxt.text=error
}
}
function checkJavaScriptReady():Boolean {
var isReady:Boolean = ExternalInterface.call("isReady");
return isReady;
}
function timerHandler(event:TimerEvent):void {
var isReady:Boolean = checkJavaScriptReady();
if (isReady) {
readyTimer.stop();
filePath = String(ExternalInterface.call("getFilePath"));
uploadTxt.text=filePath
}
}
function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
function completeHandler(event:Event):void {
uploadTxt.text="上传完毕..."
}
function openHandler(event:Event):void {
uploadTxt.text="正在上传..."
}
function progressHandler(event:ProgressEvent):void {
trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
uploadTxt.text="已上传"+int((event.bytesLoaded/event.bytesTotal)*100)+"%"
}
function securityErrorHandler(event:SecurityErrorEvent):void {
uploadTxt.text="IO错误..."
}
function httpStatusHandler(event:HTTPStatusEvent):void {
//uploadTxt.text="HTTP状态..."
}
function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
uploadTxt.text=""
}
后台ASP文件同上.
html文件:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script>
var jsReady = false;
function isReady() {
return jsReady;
}
function pageInit() {
jsReady = true;
}
function getFilePath(name) {
document.all.myForm.browFile.click()
return document.all.myForm.browFile.value;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>upLoad</title>
</head>
<body onload="pageInit();">
<!--url's used in the movie-->
<!--text used in the movie-->
<!--
<p align="left"><font face="宋体" size="12" color="#ff0000" letterSpacing="0.000000" kerning="0">建议:<font color="#000000">请选择*.jpg,*.gif,*.png并小于500k的大小!</font></font></p>
<p align="left"><font face="黑体" size="12" color="#000000" letterSpacing="0.000000" kerning="0"><b>图片</b></font></p>
<p align="left"><font face="宋体" size="12" color="#000000" letterSpacing="0.000000" kerning="0">浏览</font></p>
<p align="left"><font face="宋体" size="12" color="#000000" letterSpacing="0.000000" kerning="0">添加图片</font></p>
<p align="left"><font face="宋体" size="12" color="#ff0000" letterSpacing="0.000000" kerning="0">请勿上传色情或违法反动图片,违者删除空间! </font></p>
-->
<!-- saved from url=(0013)about:internet -->
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="400" id="upLoad" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="upLoad.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="upLoad.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="upLoad" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
<table width="100" border="1" id="tab" style="VISIBILITY: hidden">
<td>
<form name="myForm">
<input name="browFile" type="file" id="browFile" value="">
</form>
</td>
</table>
</body>
</html>