我正在 Tastypie 中编写一个 API,我想用多部分 http 请求上传文件。
我在我的 android 文件的异步任务中创建了这个函数:
public JSONObject getJSONObejctPOSTFILE(String url,
List<NameValuePair> params, String key) {
HttpClient client = HttpClientFactory.getThreadSafeClient();
HttpPost requestPOST = new HttpPost("http://192.168.0.194/api/v1/newresource/?format=json");
//the request is definied
requestPOST.setHeader("Content-type", "multipart/form-data");
requestPOST.setHeader("Accept", "application/json");
try {
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("name", new StringBody("new.jpg"));
multipartEntity.addPart("type", new StringBody("image"));
File image = new File(DataObjectToNet.getList_objects().get(0).getData());
FileBody fil = new FileBody(image);
multipartEntity.addPart("file", new FileBody(image));
requestPOST.setEntity(multipartEntity);
client.execute(requestPOST, new PhotoUploadResponseHandler());
} catch (Exception e) {
System.out.println(e.toString());
}
return null;
}
Tastypie 中的 api 是访问https://github.com/toastdriven/django-tastypie/issues/42时产生的代码。问题是当我使用 android 客户端发送文件时,服务器会给我下一个响应:
{"error_message": "Invalid boundary in multipart: None", "traceback": "Traceback (most recent call last):\n\n File \"/usr/lib/python2.7/site-packages/tastypie/resources.py\", line 192, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"/usr/lib/python2.7/site-packages/tastypie/resources.py\", line 397, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n File \"/usr/lib/python2.7/site-packages/tastypie/resources.py\", line 427, in dispatch\n response = method(request, **kwargs)\n\n File \"/usr/lib/python2.7/site-packages/tastypie/resources.py\", line 1161, in post_list\n deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))\n\n File \"/var/www/html/patit_server_app/api/api.py\", line 346, in deserialize\n data = request.POST.copy()\n\n File \"/usr/lib/python2.7/site-packages/django/core/handlers/modpython.py\", line 69, in _get_post\n self._load_post_and_files()\n\n File \"/usr/lib/python2.7/site-packages/django/http/__init__.py\", line 360, in _load_post_and_files\n self._post, self._files = self.parse_file_upload(self.META, data)\n\n File \"/usr/lib/python2.7/site-packages/django/http/__init__.py\", line 319, in parse_file_upload\n parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)\n\n File \"/usr/lib/python2.7/site-packages/django/http/multipartparser.py\", line 65, in __init__\n raise MultiPartParserError('Invalid boundary in multipart: %s' % boundary)\n\nMultiPartParserError: Invalid boundary in multipart: None\n"}
这是什么问题?android http请求它是床格式吗?我知道tastepie 将文件上传到我的服务器是一种不好的方式,但我想以这种形式执行此操作。
Tastypie API 是:
class MultipartResource(object):
def deserialize(self, request, data, format=None):
if not format:
format = request.META.get('CONTENT_TYPE', 'application/json')
if format == 'application/x-www-form-urlencoded':
return request.POST
if format.startswith('multipart'):
data = request.POST.copy()
data.update(request.FILES)
return data
return super(MultipartResource, self).deserialize(request, data, format)
class ResourceNewResource(MultipartResource,ModelResource):
fil = Base64FileField('data')
class Meta:
queryset = res.objects.all()
resource_name = 'newresource'
serializer = Serializer(formats=['json'])
allowed_methods = ['get','post']
always_return_data=True
authorization = Authorization()
有人可以帮我将android文件上传到tastepie django服务器吗?
谢谢 !!!