将文件上传到不同的服务器,无需追踪 [英] Form uploading files to different server without following

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

问题描述

如何将django中的文件发送到不同的服务器,而不会将用户重定向到服务器?所以所有这一切都是在django中重写这个简单的php函数:

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 :

$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.';
}

第二个服务器上的功能只是简单的php func,从 $ _ FILES

Function on the second server is just simple php func that reads files from $_FILES:

<?php
    move_uploaded_file(
        $_FILES['image']['tmp_name'],
        '/var/www/image/uploaded-file.jpg'
    );
    echo 'file saved.';
?>

我已经尝试过 django-filetransfers ,它的工作原理,但我不知何故不能让它停留在我上传文件的页面上。我已经编辑了upload_handler视图,文件被正确发送,但之后我被重定向到我的第二个服务器:

I've already tried django-filetransfers, 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 :

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()})

这是我的做法。我使用httplib的功能,还有来自python-poster的multipart_encode函数,它创建了我的文件头:

And here's my approach. I'm using functions from httplib and also multipart_encode function from python-poster that creates me file headers :

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

HTML:

<form action="{{ views.file_upload }}" method="POST" enctype="multipart/form-data">
    {{ form.as_p }}
  <input type="submit" value="Upload" />
</form>

因此我得到错误500并在调试中:

As a result I get 'Error 500' and in debug :

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'}

我的功能是基于python / django文档和几个我发现的解决方案在网上。功能看起来是一样的,但不知何故,它不起作用。我应该采取不同的做法吗?在php中,我不需要定义头等。

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.

推荐答案

首先我想问你为什么这样做是因为存储或加载还是故障转移?如果是存储,为什么要通过HTTP将文件发送到第二台服务器?我猜想一个简单的SSH文件传输将会很好( scp )。如果是故障切换,那么 rsync 将会更好。

Well, first of all I'd like to ask why are you doing this? Is it because of the storage or load or perhaps failover? In case it's storage, why are you sending the file to the second server via HTTP? I guess a simple SSH file transfer would be just fine (scp). In case it's failover you'll be better off with rsync.

如果使用您自己的第二台服务器不是强制性的,那么您也可以使用CDN服务。

If using your own second server is not mandatory you might as well go with a CDN service.

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

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