我尝试从android客户端上传文件到Django视图 但在上传该请求时.FILES始终为空 这是我的django视图代码: 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")
这是我的服务器文件上传AsyncTask: class ServerFileUploadTask扩展了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) {
}
}
我尝试了大多数示例但没有成功,其中大部分都使用了已弃用的方法和类
我在端口8080上使用Django 1.7和开发服务器
也使用Android 4.2版 还有其他一些使用httpentity和http实体构建器的示例 那对我不起作用
答案 0 :(得分:0)
我无法通过上述代码找到解决方案但最终通过使用解决了问题 Android异步Http客户端库