0

を使用してファイルデータをエンコードしていますjson_encode。これは、エンコードされたデータを Javascript で送信したときに得られるものです。

ここに画像の説明を入力

これは私のphpコードです(codeigniterを使用してファイルをアップロードします):

$file_info = $this->upload->data();
echo json_encode($file_info);

そして、JavaScriptファイルでデータを使用します:

'onUploadSuccess' : function(file, data, response) {
      alert('The file was saved to: ' + data);
}

エンコードされているファイル名またはその他の文字列を使用するにはどうすればよいですか?!

たとえば、次のように使用できます。

'onUploadSuccess' : function(file, data, response) {
  alert('The file name is: ' + file_name);
}
4

1 に答える 1

1

You have 3 variables in your response. To see what each one is when your code is executed, use console.log().

'onUploadSuccess' : function(file, data, response) {
    console.log('\n"data" variable:')
    console.log(data);
    console.log('"file" variable:')
    console.log(file);
    console.log('\n"response" variable:')
    console.log(response);
}

Now open up your javascript log (F12 in Chrome, Shift+F5 I think in firefox). The Json data should have been converted into an object. If it's in its json form, add JSON.parse(data).

Objects are the backbone of javascript. To select information in an object named data, you use data.property. So data.file_name should return "83274983279843.jpg", data.type would return the type, etc.

Edit: So after discussion in chat the issue was you didn't parse the JSON. Also I incorrectly told you to reverse the variable order.

Here is the fixed code:

'onUploadSuccess' : function(file, data, response) { 
    data = JSON.parse(data) 
    alert('The file : ' + data.file_type); 
}
于 2013-08-15T20:32:26.180 に答える