Phonegap file upload (Java, PHP) phpnegap file Upload FileTransfer object in phonegap introduction: http://docs.phonegap.com/en/1.6.1/cordova_file_file.m phonegap file upload (Java, PHP)
Introduction to the FileTransfer object in phonegap for uploading phpnegap files:
Http://docs.phonegap.com/en/1.6.1/cordova_file_file.md.html#FileTransfer
?
Today's code is organized by students. Write down here for future reference
?
FileTransfer? Is an object that allows you to upload files to a server or download files from a server.
?
Used to upload files to the server
?
There are examples in it, which are already very detailed. there is a section:
var options = new FileUploadOptions();options.fileKey="file";options.fileName=fileURI.substr(fileURI.lastIndexOf('/')+1);options.mimeType="text/plain";
?
Front-end full version can refer to: http://www.oschina.net/question/200138_34919
?
When uploading images today, you cannot get the file stream at the backend. The Java logic is as follows:
Int MAX_SIZE = 102400*102400; DataInputStream in = null; FileOutputStream fileOut = null; String contentType = request. getContentType (); try {if (contentType. indexOf ("multipart/form-data")> = 0) {in = new DataInputStream (request. getInputStream (); int formDataLength = request. getContentLength (); if (formDataLength> MAX_SIZE) {return;} byte dataBytes [] = new byte [formDataLength]; int byteRead = 0; int totalBytesRead = 0; while (totalBytesRead <formDataLength) {byteRead = in. read (dataBytes, totalBytesRead, formDataLength); totalBytesRead ++ = byteRead;} String file = new String (dataBytes); String saveFile = file. substring (file. indexOf ("filename = \" ") + 10); saveFile = saveFile. substring (0, saveFile. indexOf ("\ n"); saveFile = saveFile. substring (saveFile. lastIndexOf ("\") + 1, saveFile. indexOf ("\" "); int lastIndex = contentType. lastIndexOf ("="); String boundary = contentType. substring (lastIndex + 1, contentType. length (); int pos; pos = file. indexOf ("filename = \" "); pos = file. indexOf ("\ n", pos) + 1; pos = file. indexOf ("\ n", pos) + 1; pos = file. indexOf ("\ n", pos) + 1; int boundaryLocation = file. indexOf (boundary, pos)-4; // The position at which the file data is obtained. int startPos = (file. substring (0, pos )). getBytes ()). length; // The position at which the file data is obtained. int endPos = (file. substring (0, boundaryLocation )). getBytes ()). length; // write-out class of the created file fileOut = new FileOutputStream (System. getProperty ("java. io. tmpdir ") +"/aa.jpg "); // Save the file data fileOut. write (dataBytes, startPos, (endPos-startPos); fileOut. close () ;}} catch (Exception ex) {throw new ServletException (ex. getMessage ());}
? It was later discovered that a fatal parameter was missing: options. chunkedMode = false;
?
For the parsing of chunkedMode, see: http://www.issociate.de/board/post/368589/How_to_force_the_apache_transfer_the_data_in_chunked_mode ?. Html
?
The general idea is: if the file length is unpredictable, it is transmitted in the chuckedMode mode. now the image is transmitted and the size is known. I don't know why the apache server cannot handle it, make sure to set chunkedMode to false. now the upload is successful.
?
For this parameter, I also compared the php version File upload: written in this way on the php server:
"; echo "
"; } } } else { echo "No images on server"; }}?>
? Php code reference: https://github.com/brycecurtis/articles/tree/master/CameraUpload
?
Code description and explanation in: https://www.ibm.com/developerworks/mydeveloperworks/blogs/94e7fded-7162-445e-8ceb-97a2140866a9/entry/upload_a_picture_using_phonegap_on_android8? Lang = en
?
This front-end code of the php version:
// Verify server has been entered server = document.getElementById('serverUrl').value; if (server) { // Specify transfer options var options = new FileUploadOptions(); options.fileKey="file"; options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1); options.mimeType="image/jpeg"; options.chunkedMode = false; // Transfer picture to server var ft = new FileTransfer(); ft.upload(imageURI, server, function(r) { document.getElementById('camera_status').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded."; }, function(error) { document.getElementById('camera_status').innerHTML = "Upload failed: Code = "+error.code; }, options); }
? It is indeed a setting that contains chunkedMode = false and runs through the local machine.