English 中文(简体)
在文件上载时从javascript转至django的图像
原标题:passing data from javascript to django view when uploading a file

我建立了<代码>no javascript 代码,其中使用html> 表格django view上载文件。 它使用<代码>html5 输入要素选定文档。 在点击<代码>submit纽吨时,文档在无问题的情况下上载。 django 的观点可从request.FILES上查阅。

def upload_file(request,template_name):
    to_return = {}
    store_message="failure"
    if request.method ==  POST :
        if request.FILES.has_key( fselect ):
            file = request.FILES[ fselect ]
            with open( /uploadpath/%s  % file.name,  wb+ ) as dest:
                for chunk in file.chunks():
                    dest.write(chunk)
            store_message="success"
    to_return[ store_message ]= store_message
    if store_message == "failure":
        return redirect( home )
    reqctx = RequestContext(request,to_return)
    return return render_to_response(template_name,reqctx)

html

    <form enctype="multipart/form-data" method="post" action="{% url uploaded %}"> {% csrf_token %}
    <input type="file" name="fselect" id="fselect">    </input>
    <input type="submit" id="uploadbtn" value="upload">    
    </form>

Now I want to use some javascript to call the django view . I have coded ajax version of upload view

def ajax_upload_file(request):
    to_return = {}
    store_message="failure"
    if request.method ==  POST :
        if request.FILES.has_key( fselect ):
            file = request.FILES[ fselect ]
            with open( /uploadpath/%s  % file.name,  wb+ ) as dest:
                for chunk in file.chunks():
                    dest.write(chunk)
            store_message="success"
     to_return[ store_message ]= store_message
     serialized = simplejson.dumps(to_return)
     if store_message == "failure":
        return HttpResponseServerError(serialized, mimetype="application/json")
     else:
        return HttpResponse(serialized, mimetype="application/json")

令我感到困惑的是,<代码>>>>>>>>> 物体如何从<代码>javascript上通过。 code。

javascript Code

$(document).ready(function(){
  $( #fselect ).change(function(){uploadFile()});

}
function uploadSubTitleFile(){
    //check if it is a subtitle file?
    var file=document.getElementById( fselect ).files[0];
    var data = {  };//how to pass the data
    var args = { type:"POST", url:"ajax_upload/", data:data, complete:done };    
    return;
}
最佳回答

简单回答是,你没有。 将你的形式变成一个隐蔽的框架,让 j子把自己的窗户称作自己的窗户。

由于文件上载通过单体进行,因此,诸如吉大港山区地位法等传统反应数据无法直接获得,联系管理人无法合理地发现成功或失败(除交易时间外)。 相反,当交易完成时,自动卸载手将收到载有iframe文件整体的回复标,作为证明。

http://developer.yahoo.com/yui/connection/#upload

我的建议是,找到一个象样的图书馆,供你使用。

与最近的浏览器一道,可以在不依赖iframe的情况下这样做:。 与j Query.ajax的多端/格式数据

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签