0

我将 3 个参数从 AS3/flash 传递到 PHP 服务器,前 2 个参数是字符串,第三个是编码的 JPG 图像。在发送之前,图像从 byteArray 保存在本地,因此我可以检查编码是否正常,然后使用 PHP POST 与其他 2 个参数一起发送。

问题是:前 2 个参数是可以的,但第三个(jpg 编码图像)不是,我ÿÃÿà在服务器端得到类似的东西,或者这样,几个字符就是这样。同时在本地驱动器上(实际上是在发送到服务器之前) JPG 可见,可以打开并查看所有内容,但在服务器上,就像我说的那样,我看到了新的 .jpg 文件,但里面只有几个字符。

在服务器端,我正在使用以下方式写入数据:

$fh = fopen($myFile, 'w') or die("can't open file");

Flash 提供消息

错误:错误 #2101:传递给 URLVariables.decode() 的字符串必须是包含名称/值对的 URL 编码查询字符串。

所以我看到所有 3 个参数的上传都已执行,2 个可以,而第 3 个(图像)不是。

那么我做错了什么?我同时传递 2 个字符串变量和 1 个图像的问题是什么?还是带有标题的东西?

4

1 回答 1

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 文件放入网络服务器的根文件夹,图像应上传到子文件夹 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>"]
于 2012-08-09T15:42:10.347 回答