使用curl将文件上传到django服务器 [英] upload file to django server using curl

查看:745
本文介绍了使用curl将文件上传到django服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django python服务器上,我已经定制了一个用户可以上传文件的URL。现在的问题是,当我点击浏览器时,我能够上传文件,但是当我使用curl尝试同样的事情时,我无法这样做。

On Django python server, I have customized a URL where users can upload files. Now, problem is that I am successfully able to upload files when I hit the browser but when I try same thing using curl, I am not able to do so.

views.py

import json

from django.http import HttpResponse
from django.template import Context, RequestContext
from django.shortcuts import render_to_response, get_object_or_404

# -*- coding: utf-8 -*-
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

from sdm.models import Document
from sdm.forms import DocumentForm

def lists(request):
   # Handle file upload
   if request.method == 'POST':
     form = DocumentForm(request.POST, request.FILES)
     if form.is_valid():
        newdoc = Document(docfile = request.FILES['docfile'])
        newdoc.save()

        # Redirect to the document list after POST
        return HttpResponseRedirect(reverse('sdm:lists'))

else:
    form = DocumentForm() # A empty, unbound form

# Load documents for the list page
documents = Document.objects.all()

# Render list page with the documents and the form
return render_to_response(
    'sdm/lists.html',
    {'documents': documents, 'form': form},
    context_instance=RequestContext(request)
)

........
........
........
........

........ ........ ........ ........

lists.html

 <!DOCTYPE html>
 <html>
 <head>
    <meta charset="utf-8">
    <title>Minimal Django File Upload Example</title>   
 </head>
 <body>
 <!-- List of uploaded documents -->
 {% if documents %}
    <ul>
    {% for document in documents %}
        <li><a href="{{document.docfile.url }}">{{ document.docfile.name }}</a></li>
    {% endfor %}
    </ul>
 {% else %}
    <p>No documents.</p>
 {% endif %}

    <!-- Upload form. Note enctype attribute! -->
    <form action="{% url sdm:lists %}" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <p>{{form.non_field_errors }}</p>
        <p>{{form.docfile.label_tag }} {{form.docfile.help_text }}</p>
        <p>
            {{ form.docfile.errors }}
            {{ form.docfile }}
        </p>
        <p><input type="submit" name="press" value="Upload" /></p>
    </form>
 </body>
</html> 

浏览器

终端,我试过

 $ curl --request PUT --upload-file filename http://wings.spectrumserver/sdm/lists

 $ curl --form upload-file=filename  --form press=Upload 

 http:// wings. spectrumserver/sdm/lists

$ curl --upload-file filename http://wings.spectrumserver/sdm/lists
$ curl --upload-file filename press=upload http://wings.spectrumserver/sdm/lists

$ curl -H 'Expect:' -F data=@filename -F submit=Upload wings.spectrumserver/sdm/lists

// In all cases, No error but no file upload

我尝试了一些其他变体,但没有什么似乎工作出。还有一些我尝试过的其他命令给出了NO csrf token error。我还试图删除 csrf token 条目表单 html文件 setting.py 但没有任何工作。

I tried some other variations but nothing seems working out. Also some other commands I tried which gives "NO csrf token error". I also tried removing csrf token entries form html file and setting.py but nothing worked.

我都是新来的卷曲和python两者。主要目的是使用一些python脚本上传文件。我想如果我可以通过curl上传,那么相同的东西可以在python脚本与curl库中复制,所以如果这不工作,那么任何人都可以建议一些python代码上传文件到这个服务器。

I am all new to curl and python both. Main purpose is to upload file using some python script. I thought if I can upload through curl then same things can be replicated in python script with curl library so if this is not working out then can anyone suggest some python code to upload files to this server.

编辑:

$ curl -i -F name=press -F f13 wings.spectrumserver/sdm/lists
Warning: Illegally formatted input field!
curl: option -F: is badly used here
curl: try 'curl --help' or 'curl --manual' for more information

编辑2-标题响应(f13是不包含的新文件)

$ curl -i http://wings.spectrumserver/sdm/lists

HTTP / 1.1 200 OK

日期:Thu,07 Nov 2013 23:19:18 GMT
服务器:Apache / 2.2.22(Ubuntu)

Vary:Accept-Encoding
Content-Length:1263
内容类型:text / html; charset = utf-8

HTTP/1.1 200 OK
Date: Thu, 07 Nov 2013 23:19:18 GMT Server: Apache/2.2.22 (Ubuntu)
Vary: Accept-Encoding Content-Length: 1263 Content-Type: text/html; charset=utf-8





最小的Django文件上传示例



Minimal Django File Upload Example

    <ul>

        <li><a href="/media/documents/2013/10/28/templates.zip">documents/2013/10
    /28/templates.zip</a></li>

        <li><a href="/media/documents/2013/11/07/list">documents/2013/11/07/list</a>
    </li>

        <li><a href="/media/documents/2013/11/07/f1">documents/2013/11/07/f1</a></li>

        <li><a href="/media/documents/2013/11/07/f12">documents/2013/11/07/f12</a></li>

        <li><a href="/media/documents/2013/11/07/hello.html">documents/2013/11
        /07/hello.html</a></li>

    </ul>


    <!-- Upload form. Note enctype attribute! -->
    <form action="/sdm/lists" method="post" enctype="multipart/form-data">

   <!--            
   -->        <p></p>
        <p><label for="id_docfile">Select a file</label> max. 42 megabytes</p>
        <p>

            <input type="file" name="docfile" id="id_docfile" />
        </p>
        <p><input type="submit" name="press" value="Upload" /></p>
    </form>
  </body>
</html> 


推荐答案

尝试这样:

curl -i --form docfile=@localfilename http://wings.spectrumserver/sdm/lists

如果不行,请发布您的标题响应。 -i 告诉curl打印标题响应。

If doesn't work, post your header response. -i tells curl to print the header response.

这篇关于使用curl将文件上传到django服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆