Re: [jQuery-File-Upload] How to change file name before download ?

1,296 views
Skip to first unread message

Tony Abou-Assaleh

unread,
Sep 14, 2012, 1:59:02 PM9/14/12
to jquery-f...@googlegroups.com
You can send the filename from the server in the Content-Disposition HTTP header. I have sample code in python/django, but I'm sure there are plenty of examples on the web for PHP as well.

Cheers,
TAA

On Fri, Sep 14, 2012 at 1:37 PM, Hicham Radi <hicham....@gmail.com> wrote:
Hi, 

I need to change the name of the file before downloading it from the list. How can i do this?

Thanx

--
You received this message because you are subscribed to the Google Groups "jQuery-File-Upload" group.
To post to this group, send email to jquery-f...@googlegroups.com.
To unsubscribe from this group, send email to jquery-fileupl...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Hicham Radi

unread,
Sep 17, 2012, 8:18:30 AM9/17/12
to jquery-f...@googlegroups.com
With my PHP version i have " header('Content-Disposition: inline; filename="files.json"'); ". Changing (files.json) by hand dosen't give any result. What i need is to control the filename as a variable with the new file name.

If possible, can i have your sample code in python/django. thanks

Tony Abou-Assaleh

unread,
Sep 17, 2012, 1:10:03 PM9/17/12
to jquery-f...@googlegroups.com
Here is my django function that I call from the "download" view with reference to the file and a new filename.

def respond_as_attachment(request, file, filename):
    '''
    Send the `file` to the client.
    '''
    fp = open(file.path, 'rb')
    response = HttpResponse(fp.read())
    fp.close()
    if u'WebKit' in request.META['HTTP_USER_AGENT'] or u'MSIE' in request.META['HTTP_USER_AGENT']:
        # Safari 3.0 and Chrome 2.0 accepts UTF-8 encoded string directly.
        filename_header = 'filename=%s' % filename.encode('utf-8')
    else:
        # For others like Firefox, we follow RFC2231 (encoding extension in HTTP headers).
        filename_header = 'filename*=UTF-8\'\'%s' % urllib.quote(filename.encode('utf-8'))
    response['Content-Type'] = file.mimetype
    response['Content-Length'] = file.size
    response['Content-Disposition'] = 'attachment; ' + filename_header
    #response['Content-Disposition'] = '' + filename_header  # This "opens" the file, instead of forcing download.
    response['X-Content-Type-Options'] = 'nosniff'
    return response

Hope this helps,
TAA

Hicham Radi

unread,
Sep 17, 2012, 1:33:56 PM9/17/12
to jquery-f...@googlegroups.com
I use the same function in my php code, but in  jQuery-File-Upload things are different and confusing and a little hard to anderstand because of the use of Ajax and php and i don't know what else, json maybe. Anyway thank you so much for helping. I'll try again, and if i found the solution i'll post it here, maybe i can help someone. Thank you again.

Tony Abou-Assaleh

unread,
Sep 17, 2012, 4:19:40 PM9/17/12
to jquery-f...@googlegroups.com
Hi Hicham,

I don't understand what the source of confusion is. jQuery-File-Upload is essentially a pretty interface for the plain old file input. It comes with a "sample" PHP script for handling the uploads, but that's only a sample. The only thing it requires from the server side is that the upload handler on the server return a JSON with some info.

But when it comes to everything else, from storing the file, to name it, to downloading it .. we'll that's all standard PHP stuff and jQueyr-File-Upload has nothing to do with it. Perhaps if you think of the client (JS) and server (PHP) code as being decoupled, it would be easier for you to find answers to your server-side questions.

Cheers,
TAA
Reply all
Reply to author
Forward
0 new messages