简体   繁体   English

swfupload:如何解析从upload.php脚本返回的数据?

[英]swfupload: how to parse returned data from upload.php script?

Googled for about 1 hour, still cannot find how to perform this simple task! Google搜索了大约1个小时,仍然找不到如何执行此简单任务的方法!

upload.php ends up with: upload.php的最终结果是:

    echo "FILEID:" . $randomid; // Return the file id to the script

now trying to get this FILEID: 现在尝试获取此FILEID:

          function uploadSuccess(serverData) {
        alert(serverData);
      }

Alerts [object Object]. 警报[对象对象]。 Documentation does not help at all. 文档完全没有帮助。 No examples are available. 没有可用的示例。

Please help! 请帮忙!

I have used JSON with SWFUpload. 我在SWFUpload中使用了JSON。 The problem to read FILEID (@Eric suggestion) is that SWFUpload returns file name from the uploaded file (per example, client have uploaded "test.txt" to the server, this is the name of the file it returns). 读取FILEID的问题(建议使用@Eric)是SWFUpload从上传的文件返回文件名(例如,客户端已将“ test.txt”上传到服务器,这是它返回的文件名)。 It is ok, but if you have a PHP Script that rename files (to avoid replacing), then the name of file on server is not "test.txt" if this file exists, but "test(2).txt". 可以,但是如果您有一个重命名文件(避免替换)的PHP脚本,则服务器上文件的名称(如果存在)不是“ test.txt”,而是“ test(2).txt”。

In uploadSuccess , i have used: uploadSuccess ,我使用了:

.on('uploadSuccess', function(event, file, serverData){
    var responseData = jQuery.parseJSON(serverData);
    var upload_url_tmp = responseData["name"];
    [...]

On Server-Side, I have used: 在服务器端,我使用了:

$data = array('name' => $file);
    echo json_encode($data);

Please note the $file var is the name of the file saved on Server (after rename). 请注意,$ file var是保存在服务器上的文件名(重命名后)。 Worked with PHP 5.4.9 and SWFUpload 2.2.0.1 使用PHP 5.4.9和SWFUpload 2.2.0.1

Well, for one thing, I think you have your uploadSuccess input params wrong. 好吧,一方面,我认为您的uploadSuccess输入参数错误。 Documentation states: uploadSuccess(file object, server data, received response) is the correct signature. 文档状态:uploadSuccess(文件对象,服务器数据,收到的响应)是正确的签名。

So for starters, you need to throw some extra params on your success callback :) Second, serverData is whatever your server output. 因此,对于初学者来说,您需要在成功回调中添加一些额外的参数:)其次,serverData是服务器输出的任何内容。 In your example, it looks like the response is going to be { "FILEID" : "somerandomid" } (I'm assuming it's coming back as JSON, though perhaps that's a misguided assumption) 在您的示例中,响应看起来将是{“ FILEID”:“ somerandomid”}(我假设它以JSON的形式返回,尽管这可能是一个错误的假设)

In any case, to access the randomly created id, you should simply have to do 无论如何,要访问随机创建的ID,您只需要做

function uploadSuccess(fileObject, serverData, response){
    alert(serverData.FILEID); 
 }

Also, you should use Firebug, which is a plugin for Firefox. 另外,您应该使用Firebug,这是Firefox的插件。 It will let you place a breakpoint on your callback function and let you interrogate serverData to see exactly what's in it. 它可以让您在回调函数上放置一个断点,并可以查询serverData以查看其中的确切内容。

SOLVED! 解决了! It was much more simple. 这要简单得多。 Why do I always think complicated? 为什么我总是觉得复杂? function uploadSuccess(fileObject, serverData, response) { alert(serverData); 函数uploadSuccess(fileObject,serverData,response){alert(serverData); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM