开发者

How to unzip a zip file in django , which contains .shp , .prj , .shx and .dbf , thus being able to upload in the database?

I have uploaded a zip file using django in a local directory . How do I unzip it , and store it in 3 different files ?

Ok . Let me explain my problem a bit more in detail .

def upload(request):
    if request.method == 'POST':
        form = UploadF开发者_StackOverfloworm(request.POST, request.FILES)
        if form.is_valid():
            form.handle(request.FILES['file_obj'])
            #form.save() # if a modelform
            #form.cleaned_data['user'] = request.user
            z = zipfile.ZipFile('file_obj')
        for files in z.namelist():
            file(files,'wb').write(z.read(files))

        z.close()

            return render_to_response('uploaded.html', RequestContext(request,{}))
    else:
        form = UploadForm()
    return render_to_response('upload.html', RequestContext(request,{'form': form})

)

This is my upload form , which is supposed to work . file_obj contains the uploaded zip file . But it doesnt give any output .


It doesnt look like you're actually opening the upload, but a file in the current directory called 'file_obj'. You want something more like

z = zipfile.ZipFile(request.FILES['file_obj'])

also I might be wrong, but i don't think you need the form.handle() call at all, at least I've never used it, but i might stand corrected.

Also, you want to be very careful here, as you're writing out to the filenames contained in the zipfile, which for all you know could be absolute paths somewhere. You need to be very careful with that. In case I didn't say it, be careful with user created content, it might be malicious.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜