Actionscript 在服务器上使用PHP保存图像的AS3
我将3个参数从AS3/flash传递到PHP服务器,前2个参数是字符串,第3个是编码的JPG图像。在发送之前,图像保存在byteArray的本地,所以我可以检查编码是否正确,然后使用PHP POST使用其他2个参数发送 问题是:前两个参数可以,但第三个(jpg编码图像)不行,我在服务器端得到了类似Actionscript 在服务器上使用PHP保存图像的AS3,actionscript,Actionscript,我将3个参数从AS3/flash传递到PHP服务器,前2个参数是字符串,第3个是编码的JPG图像。在发送之前,图像保存在byteArray的本地,所以我可以检查编码是否正确,然后使用PHP POST使用其他2个参数发送 问题是:前两个参数可以,但第三个(jpg编码图像)不行,我在服务器端得到了类似ÃÃÃÃÃ,大约几个字符,就这样。同时在本地驱动器上(实际上是在发送到服务器之前),JPG是可见的,可以打开并查看所有内容,但在服务器上,正如我所说,我看到了新的.JPG文件,但里面只有几个字符 在服务
ÃÃÃÃÃ
,大约几个字符,就这样。同时在本地驱动器上(实际上是在发送到服务器之前),JPG是可见的,可以打开并查看所有内容,但在服务器上,正如我所说,我看到了新的.JPG文件,但里面只有几个字符
在服务器端,我正在使用以下命令写入数据:
$fh = fopen($myFile, 'w') or die("can't open file");
闪光灯发出信息
错误:错误#2101:传递给URLVariables的字符串。解码()必须
是包含名称/值对的URL编码查询字符串
所以我看到所有3个参数的上传都执行了,2个正常,第3个(图像)不正常
那么我做错了什么?问题是我同时传递2个字符串变量和1个图像?还是有标题的?上传文件时,我总是使用FileReference对象的upload()方法。下面是一个简单的ActionScript 3应用程序,它向您展示了如何使用Flash上载文件,同时通过请求传递其他参数(它们将作为GET/querystring参数传递:
package
{
import flash.display.Sprite;
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
public class FileUploader extends Sprite
{
private var request:URLRequest;
private var file:FileReference;
public function FileUploader() {
var theText:TextField = new TextField();
theText.text = "Click here to select file!";
theText.autoSize = TextFieldAutoSize.CENTER;
this.addChild(theText);
theText.addEventListener(MouseEvent.CLICK, selectFile);
request = new URLRequest();
request.url = "http://192.168.178.75/upload/upload.php";
}
private function selectFile(evt:Event):void {
file = new FileReference();
configureListeners(file);
file.browse(getTypes());
}
public function uploadImage(file:FileReference):void {
var myData:URLVariables = new URLVariables();
myData.user = "valueOfUserParam";
myData.filename = file.name;
request.data = myData;
// "filedata": in PHP, the file can be accessed using $_FILE['filedata']
file.upload(request, "filedata");
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.CANCEL, cancelHandler);
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(Event.SELECT, selectHandler);
dispatcher.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadCompleteDataHandler);
}
private function getTypes():Array {
var allTypes:Array = new Array(getImageTypeFilter(), getTextTypeFilter());
return allTypes;
}
private function getImageTypeFilter():FileFilter {
return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
}
private function getTextTypeFilter():FileFilter {
return new FileFilter("Text Files (*.txt, *.rtf)", "*.txt;*.rtf");
}
private function cancelHandler(event:Event):void {
trace("cancelHandler: " + event);
}
private function completeHandler(event:Event):void {
trace("completeHandler: " + event);
}
private function uploadCompleteDataHandler(event:Event):void {
trace("uploadCompleteData: " + event);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
trace("httpStatusHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
private function openHandler(event:Event):void {
trace("openHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
var file:FileReference = FileReference(event.target);
trace("progressHandler name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
private function selectHandler(event:Event):void {
var file:FileReference = FileReference(event.target);
trace("selectHandler: name=" + file.name + " URL=" + request.url);
this.uploadImage(file);
}
}
}
以及我用于测试的服务器端PHP代码:
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$uploaddir = $_SERVER['DOCUMENT_ROOT']."/img/";
header("content-type: text/xml");
?><result>
<?php
if (empty($_FILES)) { ?>
<message>No file has been uploaded!</message>
<?php
} else {
$filename = $_FILES['filedata']['name'];
// Mark the file with a timestamp
$filename = str_replace(".jpg", time().".jpg", $filename);
$uploadfile = $uploaddir . $filename;
$success = move_uploaded_file($_FILES['filedata']['tmp_name'], $uploadfile);
if ($success) {
?>
<message>File is valid, and was successfully uploaded</message>
<debug>
<files><![CDATA[<?php print_r($_FILES); ?>]]></files>
<post><![CDATA[<?php print_r($_POST); ?>]]></post>
<get><![CDATA[<?php print_r($_GET); ?>]]></get>
</debug>
<?php
} else {
?>
<message>File upload failed miserably</message>
<?php
}
}
?>
</result>
没有上传任何文件!
文件有效,已成功上载
]]>
]]>
]]>
文件上传失败得很惨
将PHP文件放入Web服务器的根文件夹中,图像应该上传到img子文件夹中,您必须创建该子文件夹
调试模式下的跟踪输出应如下所示:
selectHandler: name=Penny_test.jpg URL=http://localhost/upload/upload.php
openHandler: [Event type="open" bubbles=false cancelable=false eventPhase=2]
progressHandler name=Penny_test.jpg bytesLoaded=11201 bytesTotal=11201
completeHandler: [Event type="complete" bubbles=false cancelable=false eventPhase=2]
uploadCompleteData: [DataEvent type="uploadCompleteData" bubbles=false cancelable=false eventPhase=2 data="<result>
<message>File is valid, and was successfully uploaded</message>
<debug>
<files><![CDATA[Array
(
[filedata] => Array
(
[name] => Penny_test.jpg
[type] => application/octet-stream
[tmp_name] => /tmp/phpaUurZr
[error] => 0
[size] => 11201
)
)
]]></files>
<post><![CDATA[Array
(
[Filename] => Penny_test.jpg
[Upload] => Submit Query
)
]]></post>
<get><![CDATA[Array
(
[filename] => Penny_test.jpg
[user] => valueOfUserParam
)
]]></get>
</debug>
</result>"]
selectHandler:name=Penny\u test.jpg URL=http://localhost/upload/upload.php
openHandler:[事件类型=“打开”气泡=false可取消=false事件阶段=2]
progressHandler name=Penny_test.jpg bytesLoaded=11201 ByTestTotal=11201
completeHandler:[事件类型=“完成”气泡=false可取消=false事件阶段=2]
uploadCompleteData:[DataEvent type=“uploadCompleteData”气泡=false可取消=false事件阶段=2数据=”
文件有效,已成功上载
排列
(
[名称]=>Penny_test.jpg
[类型]=>应用程序/八位字节流
[tmp_name]=>/tmp/phpaUurZr
[错误]=>0
[大小]=>11201
)
)
]]>
Penny_test.jpg
[上传]=>提交查询
)
]]>
Penny_test.jpg
[用户]=>valueOfUserParam
)
]]>
"]
您能告诉我们上传图像时使用的代码吗?如何将参数添加到请求中?来自Adobe ActionScript3文档:“要向服务器发送POST或GET参数,请将URLRequest.data的值设置为参数,并将URLRequest.method设置为URLRequestMethod.POST或URLRequestMethod.GET。”制作图像后,这里有一个代码:根据您的描述,不清楚您是希望在用户可以选择文件的位置进行文件上载,还是希望在没有任何用户交互的情况下进行图像上载。我提供的答案是表单/文件选择上传场景。