开发者

Django + ExtJS FileUpload (direct from PC and from link)

Firstly - sorry for my English.. I've inherited a rather big project which uses ExtJS and is based on Django.. So I have an upload form and it works ok. The path to the file is given in a fileuploadfield (this is the xtype of ExtJS field) then it is taken by the Django and saved to the DB (it's being transformed to a needed view to be stored in the base, but that's not the problem..) So I have smth like this:

class image(entity):
    ...

    def create(self,request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        try:
            parent_id = int(request.POST["parent_id"])
        except:
            return ErrorResponse()
        if form.is_valid():
            fl =  request.FILES['file'];
            file = fl.read()
            Obj = self.data.obj
            New = Obj()

            setattr(New,'is_main',False)

and so on.. So I need to make a file upload from a direct link. Of course by another field. Having the file from a link is not a problem (urllib2 helps great), saving the file to the media folder isn't a problem too. But I can't insert it in the code above. As I understand - my problem is that the file object differs from the request.FILES thing.. But I don't know how to convert a normal file (in my case all the files are images) to the type of request.FILES so it would be readable for the next functions.. What I mean: I catch the link to the file from another field(textfield):

            url = request.POST['link'] #It get's the user posted link
            name = urllib2.urlopen(url).read()
            f = open('media/images/picture.gif', 'wb') #I overwrite it on a dummy, couse it must be than transfered to the db
            f.write(name)
            f.close #the file can be seen in the media path

Ok. The file is now in the /media/ folder, but if I simply try to replace the file = fl.read() with file=f.read() it doesn't work. As I understand - the request.FILES and just a file are different.. Can anybody hel开发者_高级运维p me to convert my file to the needed type? Or tell me how can this idea be done.. The project is really big and if I try changing the submitting mechanism it may cause a lot of problems.. Again sorry for my English)


I really don't understand why it didn't work, but I found out that this works for me..

            url = request.POST['file_link']
            name = urllib2.urlopen(url).read()
            file = name
            ..

the urllib2.urlopen("link to a file") returns a normal File and that is it..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜