Note that there are some explanatory texts on larger screens.

plurals
  1. POForm uploading files to different server without following
    primarykey
    data
    text
    <p>How can I send a file in django to a different server without user being redirected to the server ? So all goes to rewriting this simple php function in django :</p> <pre><code>$filename = 'C:/tmp/myphoto.jpg'; $handler = 'http://www.example.com/upload.php'; $field = 'image'; $res = send_file($filename, $handler, $field); if ($res) { echo 'done.'; } else { echo 'something went wrong.'; } </code></pre> <p>Function on the second server is just simple php func that reads files from <code>$_FILES</code>:</p> <pre><code>&lt;?php move_uploaded_file( $_FILES['image']['tmp_name'], '/var/www/image/uploaded-file.jpg' ); echo 'file saved.'; ?&gt; </code></pre> <p>I've already tried <a href="http://www.allbuttonspressed.com/projects/django-filetransfers" rel="nofollow">django-filetransfers</a>, and it works but I somehow cannot make it stay on the page from which I am uploading file. I have edited the upload_handler view and files are sent properly but after that I'm redirected to my second server :</p> <pre><code>def upload_handler(request): if request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponseRedirect("/upload") upload_url, upload_data = prepare_upload(request, "address of my server/") form = UploadForm() return direct_to_template(request, '/upload.html', {'form': form, 'upload_url': upload_url, 'upload_data': upload_data, 'uploads': UploadModel.objects.all()}) </code></pre> <p>And here's my approach. I'm using functions from httplib and also multipart_encode function from python-poster that creates me file headers :</p> <pre><code>def file_upload(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): f = request.FILES['file'] logging.debug(f) status = send_file(request.FILES['file']) c = RequestContext(request, { "status" : status, }) template = "management/status.html" result = render_to_string(template, c) return HttpResponse(result) else: form = UploadFileForm() return render_to_response('management/file_upload.html', {'form': form}) def send_file(file): datagen, headers = multipart_encode({"myfile": file}) conn = httplib.HTTPConnection(HOST) conn.request('POST', '/rte/', file, headers) res = conn.getresponse() if res.status != 200: logging.debug("error \n") logging.debug(file) logging.debug("\n") logging.debug(headers) return res.status </code></pre> <p>HTML:</p> <pre><code>&lt;form action="{{ views.file_upload }}" method="POST" enctype="multipart/form-data"&gt; {{ form.as_p }} &lt;input type="submit" value="Upload" /&gt; &lt;/form&gt; </code></pre> <p>As a result I get 'Error 500' and in debug :</p> <pre><code>2010-10-20 18:12:55,819 DEBUG thumb.php4.jpg 2010-10-20 18:14:55,968 DEBUG error 2010-10-20 18:14:55,968 DEBUG thumb.php4.jpg 2010-10-20 18:14:55,969 DEBUG 2010-10-20 18:14:55,969 DEBUG {'Content-Length': 15019, 'Content-Type': 'multipart/form-data; boundary=02cafbc1d080471284be55dc1095b399'} </code></pre> <p>My functions are based on python/django docs and few solutions I've found on the internet. Functionality looks the same but somehow it doesn't work. Should I take different approach ? In php I do not need to define headers etc.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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