How to name download file - url

I'm calling a web service to download a file, but it always assigns a generic name to the download. Every file I download has the same name.
So I appended "/filename.pdf?a=true" to the end of the URL as follows:
http://localhost/api/node/content/b52b3136-1cf0-48f2-a109-676a16015612/myfilename.pdf?a=true
Now on Firefox, the file downloads with the desired: myfilename.pdf.
But this isn't working on IE or Chrome.
What is wrong with this?

You want a header: Content-Disposition: attachment; filename=my_file_name.pdf
Will force a download (instead of an inline display) and set the file name. If you do not force a file name, but allow inline display (via PDF browser plugin) the filename is meaningless, which is, why it is often ignored by browsers.

Related

What is the header Content type suppose to do?

So i have this ASP.Net app and i want to allow the client to have the option to download an xlsx file, im using the HttpContext.Response to set the Content Type "application/xlsx" and the Content-Disposition sets the file name with the extension .xlsx, the download works correctly.
if I change the file name to .pdf then the file is downloaded as a pdf, when I checked the developer tool network response the Content Type was "application/xlsx" and in another test I change the name fileto .png and the file was downloaded as an image (it did not work, but it was shown as an image)
So what is the content type supposed to do, does it bring some sort of security how do i make sure that the user can only downloads the file associated to the content type?
Im using response.BinaryWrite()
A content type indicates what type of content is being transmitted over http regardless of the content name. For example as you already have tested, you can transmit a pdf file naming it as a .png file.
It is the browser or the operating system that is representing a file with the name .png as an image to you. Another software would treat a pdf file with .png name as a pdf file.
The name of the file matters after the download is complete. But the software that is handling the download (in this case a web browser) actually may need to know the original media type. And you might want to write client side codes that would need to know what type of file is being transferred from the server regardless of the file name.

Download or view the files sent as multi-part Request (PNG, PDF) through network proxy tool?

How to download or view the files sent as multi-part Request (e.g. PUT) via a software tool?
Is there any way to accomplish this with a specific tool like CharlesProxy on macOSX, to download and view files that were sent as a part of request (PUT multipart request)? I typically fix such issues by saving the file to sandbox via code changes. Ideally, need something that can be used by our QA and doesn't require any code modification.
Charles Proxy on macos is sufficient for the most dev/QA needs, such as:
Throttle network
Device debugging
Download response data
...
However, there is no option present to view or download files in HTTP request in Charles Proxy 4.x:
Charles Proxy 4.x (and earlier) allows saving response files, example pdf in this screenshot:
This can be done by editing binary file manually. It's a bit tricky, but can save the file in multipart HTTP request, without any modification to project code.
Here are the steps (verified on Charles v4.2.8 and macOS v10.12.6):
Save request. Right click a recorded HTTP request (the one that send file), and click "Save Request...". This will save the whole HTTP request in binary format.
Inspect Hex representation of request. Left click that recorded HTTP request, and click "Hex" tab of "Request" panel. This will show the binary representation of request, together with some parsed text.
Edit the saved request. Open the saved request (step 1) with editor that support binary, such as Sublime Text. Then, remove all non-image binary code according to the result of step 2. Especially, remove every bytes before (and include) the first empty line (0d0a0d0a in macOS and Windows, 0a0a in Linux), and remove the tail bytes. For example, following screenshot indicates request bytes of step 2, the selected bytes would be deleted (please note the 0d0a bytes, as this experiment is taken on Mac):
...
Save image file. Save the file after step 3 is finished. Then, append filename extension according to the Content-Type value in step 2. In this experiment, the Content-Type is image/png, so .png is appended to the filename.
That's it. You can open the xxx.png file now. It's a pure image file.
Note: this experiment only contain 1 file, but the strategy works when there are multiple file upload in request.

Rails5: Open excel file in browser

I am using shrine for excel file uploading. File is successfully uploaded and linked to model instances using shrine.
I wanted to open that Excel file(.xls, .xlsx) in browser instead of downloading it. I have looked for various solutions and tried them, but no luck.
The recommended solution from most of the people is using 'send_data', I have tried with that also:
send_file(data, type: 'application/vnd.ms-excel', filename: "#{uploaded_file.metadata["filename"]}", disposition: 'inline')
But it is downloading the file instead of opening it directly in browser.
My questions are
1) Is this opening/downloading file depends on browser settings?
2) Which one is better? To open a file in browser OR use gem like 'axlsx' to create and render excel templates in app?
Thanks!
Whether the browser will attempt to open the file in the browser or download it is determined by the Content-Disposition response header.
Content-Disposition: inline # browser will attempt to display it
Content-Disposition: attachment # browser will always download it
You can also specify the filename in both cases, should the user choose to download it:
Content-Disposition: inline; filename="table.xls"
Content-Disposition: attachment; filename="table.xls"
How to ensure that the Content-Disposition is specified to inline depends on where you're storing your files. If you're storing them on the filesystem, I think that the Rails::Static middleware already has the "inline" behaviour. If not, you can switch to download_endpoint.
If you're storing them on Amazon S3, you can specify the default :content_disposition upload option on Shrine::Storage::S3 initialization:
Shrine::Storage::S3.new(upload_options: {content_disposition: "inline"}, **options)

Can create a URL File in OneDrive Web UI but NOT correctly through Graph API

I can easily create a URL File through the O365 OneDrive web UI. Right click > New > Link. Enter the URL and save. The new item appears in the UI and when clicked, it will take you directly to the new URL, let's say it's www.google.com.
I'm attempting to duplicate this with the Graph API.
GET /me/drive/items/{folderId}/children
with:
{ name:'www.google.com.url',
file:{}
}
It creates what appears to be a link in the OneDrive UI but when clicked, it will not take you to the URL, it attempts to download a file. I've tried creating it with and without the .url suffix. The API looks pretty limited and there aren't many options to pass. I've also tried the beta API, same issue.
Links are actually a file with a special syntax similar to an INI file. From the sample you included here, it looks like you're creating an empty (0 byte) file named "www.google.com.url". Since the empty file doesn't match the format expected for a .URL file, OneDrive doesn't know what to do with it and just downloads the file.
To create a link programmatically, you would need to make a request like this:
PUT https://graph.microsoft.com/v1.0/me/drive/root/children/ShortcutToBing.url/content
Content-Type: text/plain
[InternetShortcut]
URL=https://www.bing.com
This will create a file named ShortcutToBing.url, and fill the contents of the file with the text below the request, which is the contents of a .URL file, pointing to www.bing.com.

Saving the file on a user selected location using rails

We are developing a functionality that allows the users to save the downloaded file. We are struggling to get a popup where the user can select a target location / folder to save his file. Can this be achieved using rails?
I think you're looking for send_file - it's very easy to use.
I think it depends on the content-type and similar headers you're returning to the user.
Try returning something like:
header('Content-disposition: attachment; filename=movie.mpg');
header('Content-type: video/mpeg');
EDIT: I am assuming you're able to generate headers and returning a file to the user by HTTP (no simple links to files)
I think you are trying to give something like file browser dialog box which allows client to save file on a particular location.
In case you are trying to give this from your server then I should say it is not possible due to security restriction which browser makers have applied to ensure client's security.
Another way is to let client download your browser plugin/activeX Control which basically is control over client's machine then you can do what you want i.e. something like this also.
I think without this the filetype downloaded by client is identified (based on headers) by browser and it opens the file save dialog box automatically and you cant enter into client's secure arena.
I think you want download file option. For example on hitting a URL you
want user to download a zip file code for it you can do something like
this:
class MyController < ApplicationController
def downloadzip
send_file "path_to_file", :type=>"application/zip"
end
end

Resources