i try upload file from android client to Django view but on uploading that request.FILES are always empty here is my django view code: views.py
def vw_reception_uploadimage(request, phonenumber):
if request.method == 'POST':
print request.META
try:
imagePath = '/home/user/Pictures/' + str(int(time.time() * 1000)) + '.jpg'
destination = open(imagePath, 'wb+')
for chunk in request.FILES["uploadedfile"].chunks():
destination.write(chunk)
destination.close()
except Exception as e:
print e
print request.FILES
return HttpResponse("ok")
and here is my server file upload AsyncTask: class ServerFileUploadTask extends AsyncTask {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "----*****";
private Activity activity;
private String filepath;
ServerFileUploadTask(Activity activity,String filepath)
{
this.activity=activity;
this.filepath=filepath;
}
@Override
protected Void doInBackground(String... uri) {
long length = 0;
int progress;
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 2048 * 1024;// 256KB
try {
FileInputStream fileInputStream = new FileInputStream(new File(
filepath));
File uploadFile = new File(this.filepath);
long totalSize = uploadFile.length(); // Get size of file, bytes
URL url = new URL(uri[0]);
connection = (HttpURLConnection) url.openConnection();
// Set size of every block for post
connection.setChunkedStreamingMode(2048 * 1024);// 256KB
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
if(PrefSingleton.getInstance().readPreference("token", null)!=null){
connection.setRequestProperty("AUTHORIZATION" , "Token "+PrefSingleton.getInstance().readPreference("token", null));
}
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Charset", "UTF-8");
connection.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
outputStream = new DataOutputStream(
connection.getOutputStream());
outputStream
.writeBytes(twoHyphens + boundary + lineEnd+"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
+ this.filepath + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
length += bufferSize;
progress = (int) ((length * 100) / totalSize);
publishProgress(progress);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
fileInputStream.close();
outputStream.flush();
outputStream.close();
} catch (Exception ex) {
}
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
i tried most examples with no success and most of them use methods and class that are deprecated
i using Django version 1.7 and development server on port 8080
also using android version 4.2 there is some other examples that use httpentity and http entity builder that don't work for me
i can't find solution with above code but ended up solving problem by using Android Asynchronous Http Client library
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.