Download a file with URLDownloadToFile - delphi

I use URLDownloadToFile to download a file in Delphi. In the url there is not the real name of the file. Is it possible to specify just the path of the file, keeping the default name that i.e. Explorer show?

You are in a catch-22 situation. You need to give URLDownloadToFile() a filename, but you have to request the URL first to discover if it has its own filename.
You have two choices:
Send a separate HEAD request to the URL first and check the Content-Disposition response header, if present. You can use HttpSendRequest() and HttpQueryInfo() for that, or any other HTTP library. You can then format a filename as needed, and then download the URL to that filename.
Use a temp filename for the download, then check the Content-Disposition response header, if present, and rename the file if needed. To get the response headers from URLDownloadToFile() you have to write a class that implements the IBindStatusCallback and IHttpNegotiate COM interfaces, then pass an instance of that class to the lpfnCB parameter. The response headers will be passed to your IHttpNegotiate.OnResponse() implementation.

Related

Simluate a file upload POST request via Postman

I'm developing a file upload method and I wanna test it independently of a frontend interface.
Normally I would use an HTML form to send a file using the 'Content-Type': 'multipart/form-data' headers.
Now as I get it, the files are converted to base64 and sent as strings.
I'm using this tool to convert a file to base64, but with both multipart or form-data headers Postman gives me this error:
431 Request Header Fields Too Large
What am I missing?
You don't need to encode the file yourself, you need to use form-data, change key type from text to file and select the file as below:

MIME media type (content type) for representing a URL?

I'm looking for the correct MIME media type (or content type) for documents that contain a single URL such as http://example.com/123/abc.
I checked IANA's MIME type registry, but couldn't find a suitable one for a URL or URI.
Should I create a custom MIME type for this purpose or use the closest registered that applies? It will be used by a RESTful API that returns a URL in the HTTP body for specific calls, to differentiate from others that do not.
You should be able to use text/uri-list defined in RFC 2483 Section 5. You are only returning a single URL, but there is nothing in the spec saying it can't be a list of one. To conform to the spec, just make sure the URL is all on one line and terminated with a CRLF pair.
text/x-uri should be fine, it's the most used as far as I know.

ASIHTTPRequest always use defaultresponseEncoding insted of Encoding mentioned in the XML file

Problem is that, while downloading an XML file using ASIHTTPRequest it use default response encoding instead of using the encoding in "encoding" attribute in the header.
The class use header information to set the responseEncoding to the particular encoding in "Charset". So the class works well in case of a HTML page as it include the encoding type in the header itself.
The default encoding is NSISOLatin1StringEncoding but the encoding in the attribute is UTF-8, which render response string like "función" instead of "función".
So I want responseEncoding property of request(ASIHTTPRequest) to be set to the encoding type in the XML file
If it is your NSLog statement output then it is correct and the ASIHTTPRequest class is working correct only
ASIHTTPRequest does not parse your xml file, it decides the encoding based solely on the contents of the http headers.
Use request.responseData to get the raw data instead and do the conversion to string yourself.

HTTP HEAD Request and System.Web.Mvc.FileResult

I'm using BITS to make requests to a ASP.NET MVC controller method named Source that returns a FileResult. I know the type FilePathResult uses HttpResponse.TransmitFile, but I don't know if HttpResponse.TransmitFile actually writes the file to the response stream regardless of the request type.
My question is, does FileResult only include the header information on HEAD requests, or does it transmit the file regardless of the request type? Or, do I have to account for HEAD requests myself?
The result is forced to react on a request by YOUR ACTION CODE. If you do not do anything special on different request types (e.g. [HttpGet]-Attribute, HttpMethodConstraints in the Route, etc...) The file is just written to the response stream.

why ?format=csv files doesn't have extension

I have a standard restful rails application.
format.html { #users = User.find(:all, :limit => 10)}
format.csv { #users = User.find(:all, :limit => 10) }
When the url is
http://localhost:3000/users.csv
I get a file with name users.csv .
However if the url is
http://localhost:3000/users?format=csv
then the file I get has name users. I would like to have file name to be users.csv .
This is the browser's default file naming coming into play. The browser doesn't know anything about the meaning of the format parameter. It just sees the resource being accessed is called 'users', so it defaults to that file name.
In the former example, the resource being requested is called users.csv, so it uses that as the default file name.
You may also want to look into the Content-Disposition HTTP header. This will cause the browser to prompt the user to save the file with a specified file name as the default (the user is free to change that though), instead of displaying the file in the browser. Thus, you could have your resource be http://localhost:3000/users?format=csv, but default the file name to foo.csv with this header:
Content-disposition: attachment; filename=foo.csv
Check out this Microsoft link for some more information. The concept is the same for rails as it is for any HTTP technology.
Set the Content-Disposition response header to attachment; filename=users.csv.
This is recognizeable as default MSIE behaviour. It ignores the filename parameter of the Content-Disposition header (if you have set any yourself). You'll really need to append the full filename as pathinfo of the URL if you want to get it to work in that browser as well. All other browsers respects the Content-Disposition header as expected.
You can send a Content-disposition header with a filename parameter to suggest a default filename to the user. For example, Content-dispostion: attachment; filename=users.csv.
By default, the browser will usually use the last component of the path portion of the URL (the part before the query, which begins with ?) as the filename. Some browsers, like Safari, will also add an extension based on the MIME type if they don't believe the current extension matches the MIME type, and they know what extension to use.

Resources