Phonegap + Laravel 4如何上传文件 [英] Phonegap + Laravel 4 How to upload file
本文介绍了Phonegap + Laravel 4如何上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我自己回答了这个问题,以便其他人不会面对我所做的问题。
I answered this question myself so that others don't face the problems i did.
推荐答案
Side(Javascript代码):
我花了一段时间想出在服务器上使用laravel 4时如何使用phonegap中的文件上传功能。这适用于可能寻求帮助的其他人:
确保:
The Client Side(Javascript Code) :
It took me a while to figure out how to use the file upload feature in phonegap while using laravel 4 on the server. This is for others who might be looking for some help :
Ensure that :
- public / images (或您要上传fie文件夹的任何其他目标文件夹是可写的,否则您会收到错误代码:1
- .fileKey 必须匹配
Input :: file(options.fileKey) - > move()
- public/images(or any other destination folder you wish to upload fie folder is writable or else you will get error code : 1
- Values of options.fileKey must match
Input::file(options.fileKey)->move()
- You make a post request.
$('。upload'
{
$('.upload').on('click',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);
}
服务器端)代码
The Server Side(Laravel) Code
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 。
That is all that is required on the server side. After a successful upload the success callback function win will run.
这篇关于Phonegap + Laravel 4如何上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文