【发布时间】:2013-10-09 22:52:48
【问题描述】:
我自己回答了这个问题,这样其他人就不会遇到我遇到的问题。
【问题讨论】:
标签: cordova laravel laravel-4 phonegap-plugins
我自己回答了这个问题,这样其他人就不会遇到我遇到的问题。
【问题讨论】:
标签: cordova laravel laravel-4 phonegap-plugins
客户端(Javascript 代码):
我花了一段时间才弄清楚如何在服务器上使用 laravel 4 时在 phonegap 中使用文件上传功能。这适用于可能正在寻求帮助的其他人:
确保:
Input::file(options.fileKey)->move()
$('.upload').on('点击',function() {
navigator.camera.getPicture(cameraSuccess,cameraFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI, mediaType: Camera.MediaType.PICTURE, sourceType : Camera.PictureSourceType.PHOTOLIBRARY }); }); function cameraSuccess(imageURI) { //set file upload options var options = new FileUploadOptions(); options.fileKey="file"; options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1); options.mimeType="image/jpeg"; options.chunkedMode = false; var ft = new FileTransfer(); ft.upload(imageURI, encodeURI(url+'/file-upload'), win, fail, options); function win(r) { console.log("Code = " + r.responseCode); console.log("Response = " + r.response); console.log("Sent = " + r.bytesSent); } function fail(error) { console.log("An error has occurred: Code = " + error.code); console.log("upload error source " + error.source); console.log("upload error target " + error.target); }
服务器端(Laravel)代码
Route::post('/file-upload',function()
{
//I am storing the image in the public/images folder
$destinationPath = 'images/';
$newImageName='MyImage.jpg';
//Rename and move the file to the destination folder
Input::file('file')->move($destinationPath,$newImageName);
}
这就是服务器端所需的全部内容。上传成功后,成功回调函数win会运行。
【讨论】: