jquery - Custom render returning binary data to ajax call in django-rest framework -
i making ajax call rest api , expect raw binary data.
$.ajax({ url: 'verify', type: 'post', contenttype: "application/json; charset=utf-8", data: json.stringify({ "url": url, }), datatype: "application/octet-stream", success: function(response, textstatus, jqxhr) { if (response) { try { // } catch (e) { displayerror(e); } } }, error: function(jqxhr, textstatus, errorthrown) { // loader.remove(); // $.unblockui(); console.log(textstatus); // parsererror console.log(errorthrown); // no conversion text application/octet-stream var err = parseerror(jqxhr, textstatus, errorthrown); displayerror(err); } });
the restful api is
from rest_framework.renderers import baserenderer class binaryrenderer(baserenderer): media_type = 'application/octet-stream' charset = none format = '.bin' def render(self, data, media_type=none, renderer_context=none): return data rest_framework.response import response import urllib2 class myview(apiview): renderer_classes = (binaryrenderer,) def post(self, request): url = request.data['url'] conn = urllib2.urlopen(url) byte = conn.read() conn.close() return response({'blob':byte})
the server returns code 200, ajax jumps error due parseerror
, errorthrown
shows no conversion text application/octet-stream
.
why content-type of response text
, not octet-stream
?
thank you
i had issue - getting binary data via ajax.
jsziputils.getbinarycontent saved day.
jsziputils.getbinarycontent("path/to/file.zip", function (err, data) { if(err) { throw err; // or handle error } // data });
Comments
Post a Comment