Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to save file data from POST variable and load it back to response in Python Django?
    primarykey
    data
    text
    <p>I have a such problem - I am using Python 2.6 / Django 1.3 and I need to <strong>accept as POST variable with key 'f', which contains a binary data</strong>. After that, I need to <strong>save data</strong> in a file.</p> <p><strong>POST</strong></p> <pre><code>T$topX$objectsX$versionY$archiverО©ҐR$0О©ҐО©ҐО©Ґull_=&lt;---------------------- content of file --------------------&gt;О©ҐО©Ґ_NSKeyedArchive(258:=CО©ҐО©Ґ </code></pre> <p><strong>Code</strong></p> <pre><code>from django.core.files.storage import default_storage from django.core.files.base import ContentFile def save(request): upload_file = request.POST['f'] save_path = default_storage.save('%s%s' % (save_dir, filename), ContentFile(upload_file)) </code></pre> <p>When I am trying to do</p> <pre><code>nano /tmp/myfile.zip </code></pre> <p>It returns data like </p> <pre><code>T^@^@^@$^@^@^@t^@^@^@o^@^@^@p^@^@^@X^@^@^@$^@^@^@o^@^@^@b^@^@^@j^@^@^@e^@^@^@c^@^@^@t^@^@^@s^@^@^@X^@^@^@$^@^@^@v^@^@^@e^@^@^@r^@^@^@s^@^@^@i^@^@$ </code></pre> <p>When its done, I am going to <strong>read saved file</strong> </p> <pre><code>def read(request): user_file = default_storage.open(file_path).read() file_name = get_filename(file_path) response = HttpResponse(user_file, content_type = 'text/plain', mimetype = 'application/force-download') response['Content-Disposition'] = 'attachment; filename=%s' % file_name response['Content-Length'] = default_storage.size(file_path) return response </code></pre> <p>In case, when I am writing </p> <pre><code>print user_file </code></pre> <p>It returns a correct data, but when I am returning a HttpResponse it has a different data from a source</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload