As Chris Hanson said MIME types are controlled by the IANA. This is detailed in RFC 2048 - Multipurpose Internet Mail Extensions (MIME) Part Four: Registration Procedures. According to RFC 3778, which is cited by the IANA as the definition for "application/pdf",
The "x." tree may be used for media types intended exclusively for usein private, local environments and only with the active agreement ofthe parties exchanging them. Types in this tree cannot be registered.
mime type php pdf download
According to the previous version of RFC 6838 - obsoleted RFC 2048(published in November 1996) it should rarely, if ever, be necessaryto use unregistered experimental types, and as such use of both "x-"and "x." forms is discouraged. Previous versions of that RFC - RFC1590 and RFC 1521 stated that the use of "x-" notation for thesub-type name may be used for unregistered and private sub-types, butthis recommendation was obsoleted in November 1996.
So its clear that the standard type MIME type application/pdf is the appropriate one to use while you should avoid using the obsolete and unregistered x- media type as stated in RFC 2048 and RFC 6838.
Technically Content-Length is optional but it is important if you want the user to be able to keep track of the download progress, and detect if the download was interrupted before the end. When using it you have to make sure you won't be send anything along with the file data. Make sure there is absolutely nothing before , not even an empty line.
There is no problem with my Chrome settings: I try addresses of other PDF files, and Chrome behaves as expected (I have it set to use Chrome's built-in PDF viewer). But every time I try the same problematic address, Chrome downloads the PDF and then displays a blank page.
This tells the browser to always download (attachment) the file, and to give it the default filename of Schubert-Sonata-21-B-flat.pdf rather than inferring it from the URL. Additionally, it does tell the browser (correctly) that it's an application/pdf file - but since it's an attachment the browser will still default to downloading.
When a Content-Disposition is inline (or unspecified), the browser will try to open the file in the default embedded viewer. This only works when the browser knows what file type it is, and the browser knows how to open that type.
The file type can be specified by the server with a Content-Type header. For example, the most common inline types are text/html, application/javascript and text/css, making up the three major parts of a modern website. You can also have more esoteric types like application/pdf.
Another possibility is the server has specified a Content-Type of application/octet-stream. This is the most generic type, and it tells the browser that the file is just arbitrary data - at which point the only thing the browser can do is download it (in theory - we'll get to that).
When a Content-Type is not specified by the server (and sometimes even when it is), the browser can perform what is known as sniffing to try to guess the type by reading the file and looking for patterns.
Upon receiving a file with an inline or unspecified disposition, the browser needs to try to open it within the browser if possible. To do this, it looks at the file type, and if it recognises the type it will try to open it. Most browsers will open any text/ type in a simple text viewer, will try to render text/html as a webpage, might open application/json in a special syntax-highlighted viewer, etc..
The type application/octet-stream was handled specially. Since it's supposed to be the most generic type, denoting an arbitrary stream of bytes, there isn't supposed to be any handler that can apply to all files of this "type". For example, in Firefox, this manifests as an inability to set the default handler for application/octet-stream.
Some websites have also used non-standard types. I've seen application/force-download used - which ends up as a download because the browser does not recognise or know what else to do with the type, but does not enjoy the special handling that application/octet-stream does.
It used to be possible to extend browser functionality with far more control than what you can do with limited extensions/addons these days. Those were most generically known as plugins. In Internet Explorer, they were ActiveX controls; in Mozilla Firefox and later Google Chrome they were NPAPI plugins. These plugins were capable of doing everything any other program could, and could additionally register themselves as a handler for a specific file type that might be otherwise unrecognised by the browser. (Incidentally, this was later found to be a huge security risk and support for these powerful plugins was gradually dropped...)
In the days of plugins, you would go and install Adobe Acrobat Reader, which would then install an ActiveX or NPAPI plugin that would register the application/pdf MIME type and tell the browser to open those types inline using the plugin.
I found an explanation. According to an answer I found, it appears that Chrome will download a PDF if the MIME content type is set not to application/pdf but rather an "incorrect or generic MIME type", application/octet-stream.
Furthermore, "Most web servers send unknown-type resources using the default application/octet-stream MIME type. For security reasons, most browsers do not allow setting a custom default action for such resources, forcing the user to store it to disk to use it."
To download a blob file stored on Drive, use the files.get method with the ID of the file to downloadand the alt=media URL parameter. The alt=media URL parameter tells theserver that a download of content is being requested as an alternative responseformat.
: b/19236190 */ display: none; } .kd-tabbed-horz > article > pre /* Remove extra spacing */ margin: 0; devsite-selector > section[active] /* Remove code section padding */ padding: 0; .filepath color: #fff; margin: 6px; max-width: calc(100% - 160px); /* Give at least 160px for the "View on GitHub" button. */ text-overflow: ellipsis; text-shadow: rgba(0,0,0,0.1) 1px 1px; overflow: hidden; .view-on-github text-shadow: rgba(12,12,12,0.1) 1px 1px; .github-docwidget-include border-radius: 0 !important; margin: 0 -1px; drive/snippets/drive_v3/src/main/java/DownloadFile.java View on GitHub import com.google.api.client.googleapis.json.GoogleJsonResponseException;import com.google.api.client.http.HttpRequestInitializer;import com.google.api.client.http.javanet.NetHttpTransport;import com.google.api.client.json.gson.GsonFactory;import com.google.api.services.drive.Drive;import com.google.api.services.drive.DriveScopes;import com.google.auth.http.HttpCredentialsAdapter;import com.google.auth.oauth2.GoogleCredentials;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Arrays;/* Class to demonstrate use-case of drive's download file. */public class DownloadFile /** * Download a Document file in PDF format. * * @param realFileId file ID of any workspace document format file. * @return byte array stream if successful, @code null otherwise. * @throws IOException if service account credentials file not found. */ public static ByteArrayOutputStream downloadFile(String realFileId) throws IOException /* Load pre-authorized user credentials from the environment. TODO(developer) - See for guides on implementing OAuth2 for your application.*/ GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() .createScoped(Arrays.asList(DriveScopes.DRIVE_FILE)); HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); // Build a new authorized API client service. Drive service = new Drive.Builder(new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) .setApplicationName("Drive samples") .build(); try OutputStream outputStream = new ByteArrayOutputStream(); service.files().get(realFileId) .executeMediaAndDownloadTo(outputStream); return (ByteArrayOutputStream) outputStream; catch (GoogleJsonResponseException e) // TODO(developer) - handle error appropriately System.err.println("Unable to move file: " + e.getDetails()); throw e; Python /* Remove extra DevSite2 margin */ .github-docwidget-gitinclude-code devsite-code, devsite-selector>section>devsite-code, devsite-selector>section>.github-docwidget-include, devsite-selector>section>.github-docwidget-gitinclude-code>devsite-code margin: 0; /* Disables includecode margin */ .github-docwidget-gitinclude-code .prettyprint margin: 0; .ds-selector-tabs > section > p /* Remove extra : b/19236190 */ display: none; .kd-tabbed-horz > article > pre /* Remove extra spacing */ margin: 0; devsite-selector > section[active] /* Remove code section padding */ padding: 0; .filepath color: #fff; margin: 6px; max-width: calc(100% - 160px); /* Give at least 160px for the "View on GitHub" button. */ text-overflow: ellipsis; text-shadow: rgba(0,0,0,0.1) 1px 1px; overflow: hidden; .view-on-github text-shadow: rgba(12,12,12,0.1) 1px 1px; .github-docwidget-include border-radius: 0 !important; margin: 0 -1px; drive/snippets/drive-v3/file_snippet/download_file.py View on GitHub from __future__ import print_functionimport ioimport google.authfrom googleapiclient.discovery import buildfrom googleapiclient.errors import HttpErrorfrom googleapiclient.http import MediaIoBaseDownloaddef download_file(real_file_id): """Downloads a file Args: real_file_id: ID of the file to download Returns : IO object with location. Load pre-authorized user credentials from the environment. TODO(developer) - See for guides on implementing OAuth2 for the application. """ creds, _ = google.auth.default() try: # create drive api client service = build('drive', 'v3', credentials=creds) file_id = real_file_id # pylint: disable=maybe-no-member request = service.files().get_media(fileId=file_id) file = io.BytesIO() downloader = MediaIoBaseDownload(file, request) done = False while done is False: status, done = downloader.next_chunk() print(F'Download int(status.progress() * 100).') except HttpError as error: print(F'An error occurred: error') file = None return file.getvalue()if __name__ == '__main__': download_file(real_file_id='1KuPmvGq8yoYgbfW74OENMCB5H0n_2Jm9')Node.js /* Remove extra DevSite2 margin */ .github-docwidget-gitinclude-code devsite-code, devsite-selector>section>devsite-code, devsite-selector>section>.github-docwidget-include, devsite-selector>section>.github-docwidget-gitinclude-code>devsite-code margin: 0; /* Disables includecode margin */ .github-docwidget-gitinclude-code .prettyprint margin: 0; .ds-selector-tabs > section > p /* Remove extra : b/19236190 */ display: none; .kd-tabbed-horz > article > pre /* Remove extra spacing */ margin: 0; devsite-selector > section[active] /* Remove code section padding */ padding: 0; .filepath color: #fff; margin: 6px; max-width: calc(100% - 160px); /* Give at least 160px for the "View on GitHub" button. */ text-overflow: ellipsis; text-shadow: rgba(0,0,0,0.1) 1px 1px; overflow: hidden; .view-on-github text-shadow: rgba(12,12,12,0.1) 1px 1px; .github-docwidget-include border-radius: 0 !important; margin: 0 -1px; drive/snippets/drive_v3/file_snippets/download_file.js View on GitHub /** * Downloads a file * @paramstring realFileId file ID * @returnobj file status * */async function downloadFile(realFileId) // Get credentials and build service // TODO (developer) - Use appropriate auth mechanism for your app const GoogleAuth = require('google-auth-library'); const google = require('googleapis'); const auth = new GoogleAuth( scopes: ' ', ); const service = google.drive(version: 'v3', auth); fileId = realFileId; try const file = await service.files.get( fileId: fileId, alt: 'media', ); console.log(file.status); return file.status; catch (err) // TODO(developer) - Handle error throw err; PHP /* Remove extra DevSite2 margin */ .github-docwidget-gitinclude-code devsite-code, devsite-selector>section>devsite-code, devsite-selector>section>.github-docwidget-include, devsite-selector>section>.github-docwidget-gitinclude-code>devsite-code margin: 0; /* Disables includecode margin */ .github-docwidget-gitinclude-code .prettyprint margin: 0; .ds-selector-tabs > section > p /* Remove extra : b/19236190 */ display: none; .kd-tabbed-horz > article > pre /* Remove extra spacing */ margin: 0; devsite-selector > section[active] /* Remove code section padding */ padding: 0; .filepath color: #fff; margin: 6px; max-width: calc(100% - 160px); /* Give at least 160px for the "View on GitHub" button. */ text-overflow: ellipsis; text-shadow: rgba(0,0,0,0.1) 1px 1px; overflow: hidden; .view-on-github text-shadow: rgba(12,12,12,0.1) 1px 1px; .github-docwidget-include border-radius: 0 !important; margin: 0 -1px; drive/snippets/drive_v3/src/DriveDownloadFile.php View on GitHub use Google\Client;use Google\Service\Drive;function downloadFile() try $client = new Client(); $client->useApplicationDefaultCredentials(); $client->addScope(Drive::DRIVE); $driveService = new Drive($client); $realFileId = readline("Enter File Id: "); $fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M'; $fileId = $realFileId; $response = $driveService->files->get($fileId, array( 'alt' => 'media')); $content = $response->getBody()->getContents(); return $content; catch(Exception $e) echo "Error Message: ".$e; .NET /* Remove extra DevSite2 margin */ .github-docwidget-gitinclude-code devsite-code, devsite-selector>section>devsite-code, devsite-selector>section>.github-docwidget-include, devsite-selector>section>.github-docwidget-gitinclude-code>devsite-code margin: 0; /* Disables includecode margin */ .github-docwidget-gitinclude-code .prettyprint margin: 0; .ds-selector-tabs > section > p /* Remove extra : b/19236190 */ display: none; .kd-tabbed-horz > article > pre /* Remove extra spacing */ margin: 0; devsite-selector > section[active] /* Remove code section padding */ padding: 0; .filepath color: #fff; margin: 6px; max-width: calc(100% - 160px); /* Give at least 160px for the "View on GitHub" button. */ text-overflow: ellipsis; text-shadow: rgba(0,0,0,0.1) 1px 1px; overflow: hidden; .view-on-github text-shadow: rgba(12,12,12,0.1) 1px 1px; .github-docwidget-include border-radius: 0 !important; margin: 0 -1px; drive/snippets/drive_v3/DriveV3Snippets/DownloadFile.cs View on GitHub using Google.Apis.Auth.OAuth2;using Google.Apis.Download;using Google.Apis.Drive.v3;using Google.Apis.Services;namespace DriveV3Snippets // Class to demonstrate use-case of drive's download file. public class DownloadFile /// /// Download a Document file in PDF format. /// /// file ID of any workspace document format file. /// byte array stream if successful, null otherwise. public static MemoryStream DriveDownloadFile(string fileId) try /* Load pre-authorized user credentials from the environment. TODO(developer) - See for guides on implementing OAuth2 for your application. */ GoogleCredential credential = GoogleCredential .GetApplicationDefault() .CreateScoped(DriveService.Scope.Drive); // Create Drive API service. var service = new DriveService(new BaseClientService.Initializer HttpClientInitializer = credential, ApplicationName = "Drive API Snippets" ); var request = service.Files.Get(fileId); var stream = new MemoryStream(); // Add a handler which will be notified on progress changes. // It will notify on each chunk download and when the // download is completed or failed. request.MediaDownloader.ProgressChanged += progress => switch (progress.Status) case DownloadStatus.Downloading: Console.WriteLine(progress.BytesDownloaded); break; case DownloadStatus.Completed: Console.WriteLine("Download complete."); break; case DownloadStatus.Failed: Console.WriteLine("Download failed."); break; ; request.Download(stream); return stream; catch (Exception e) // TODO(developer) - handle error appropriately if (e is AggregateException) Console.WriteLine("Credential Not found"); else throw; return null; This code sample uses a library method that adds the alt=media URL parameterto the underlying HTTP request. 2ff7e9595c
Comentarios