基本上我试图发布用户选择到我服务器的图像并将其保存在那里。
PHP文件:
<?php
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/config.php';
$image = $_POST["avatar"];
$user_id = $_POST["user_id"];
$files = glob('avatars/*.jpg');
$name = count($files) + 1;
$decodedImage = base64_decode($image);
file_put_contents("avatars/" . $name . ".jpg", $decodedImage);
?>
的Android
private class UploadFileToServer extends AsyncTask<Bitmap, Void, Void> {
@Override
protected Void doInBackground(Bitmap... params) {
Bitmap bitmap = params[0];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String base64avatar = Base64.encodeToString(byteArray, Base64.DEFAULT);
String dataString = "user_id=" + userID + "&avatar=" + base64avatar;
try {
URL url = new URL(URL_OMITTED);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
os.writeBytes(dataString);
os.flush();
os.close();
Log.d("Response code",conn.getResponseCode() + "");
conn.disconnect();
} catch(IOException e) {
e.printStackTrace();
}
return null;
}
}
文件在服务器上创建,但不可读。当我将compressformat设置为PNG时,奇怪地创建了一个可读文件,但它的损坏使其大部分是黑色而不是上传的图像。
答案 0 :(得分:0)
结果显示base64不是URL_SAFE。
通过更改public function add()
{
$service = $this->Services->newEntity();
if ($this->request->is('post')) {
$service = $this->Services->patchEntity($service, $this->request->data);
if ($this->Services->save($service)) {
// Last inserted service id
$service_id = $service->id;
$serviceTime = $this->Services->ServiceTimes->newEntity();
$serviceTime->service_id = $service_id;
$serviceTime->start_time = $this->request->data['serviceTimes']['start_time'];
$serviceTime->end_time = $this->request->data['serviceTimes']['end_time'];
if ($this->Services->ServiceTimes->save($serviceTime)) {
$this->Flash->success(__('The service has been saved.'));
return $this->redirect(['action' => 'index']);
}
} else {
$this->Flash->error(__('The service could not be saved. Please, try again.'));
}
}
$this->set(compact('service'));
$this->set('_serialize', ['service']);
$this->viewBuilder()->theme('ThemeAdmin');
}
并添加
$('#ex_basic_1').intimidatetime({
previewFormat: 'yyyy-MM-dd HH:mm:ss:l',
format: "yyyy-MM-dd HH:mm:ss:l"
})
到php文件解码URL_SAFE字符串。
我确定这可能不是这样做的,但它确实有效。