English 中文(简体)
利用javascript和django将文本文档上载到服务器中某些倍数。
原标题:using javascript and django to upload a text file to some folder in server

我需要上载一个包含一些数据的文字文件,从我的“条形”栏目“地方档案系统:>>,到“条形”栏目“射线”>。 这份档案将由我的“条形表>django>>/条形图中的一种功能处理。

我在html网页上创建了<input category=“file” id=“fselect” >。 我创建了一个javascript文档,试图将上载功能称作如下。

 $(document).ready(function(){
      ...
      $( #fselect ).change(function(){ 
         file=$( #fselect ).get(0).files[0];
         uploadFile(file);
      }

});

当我使用火力时,审判

file=$( #fselect ).get(0).files[0]

我能够拿到文件标本,该标本是利用输入要素选定的文本文档。 我怎么能用这个档案物体来看待django问题? django认为,这一档案标的是什么数据类型?

def storeAndProcessFile(request,file):
    pass
最佳回答

在我拿到任何 j印之前,首先,你应了解档案如何普遍上载和使用詹戈处理。 审议以下表格:

<form method="post" action="/foo/url/">
    <input type="file" name="foofile">
    <input type="submit" value="Upload">
</form>

So when the user clicks submit button, the browser will upload that file to the /foo/url/ using HTTP method POST. Then on the server side in Django, the url /foo/url/ will be mapped to some view. In your case it will be storeAndProcessFile. So what the view will have to do is take the file which was uploaded and store that to disk (or possibly some other storage system).

现在,这种观点并没有像你在提问中所显示的那样,成为其功能参数之一。 这是因为GET、POST和FILE数据是如何在吉大港山区应用方案的要求中通过的。 因此,档案实际上将成为“/code>参数”的一部分。 您可以通过<条码>查询查阅文件。

To just store the file to disk, your view might look something like:

def storeAndProcessFile(request):
    # make sure the the request method is POST
    if request.method !=  POST :
        return HttpResponseBadRequest( Only POST requests are allowed )
    # now get the uploaded file
    file = request.FILES[ foofile ]
    # the file is going to be an instance of UploadedFile
    with open( /destination/path/%s  % file.name,  wb+ ) as dest:
        for chunk in file.chunks():
            dest.write(chunk)
    # return success message
    return HttpResponse( File uploaded )

这部法典在Django文件中占很大比例。 如欲了解更多情况,请见here

至于Javascript,有 j窗,可以一度用ja子或甚至多份文档上载。 我真的喜欢 。 它有许多特点,包括发送多个档案。 然而,如果这太大,那么你就只能做一只 j子,而且有 to子,但我最近没有检测过任何其他人,因此可以提出任何建议。

然而,在要求Django站点(即CSRF)时,应当牢记一点。 自Django 1.2.5以来,它们改变了CSRF的验证方式,使许多上载图书馆破碎。 如果你不需要担心的话,你可以永远添加<条码>csrf_exempt<>/条码>。 更正:

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def storeAndProcessFile(request):
    ...

但是,如果需要CSRF,你可以研究将j Query文档上载荷与CSRF连接起来的样本执行情况,here

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签