javascript check before download a file - php

I've a need that I didn't think to be so weird, but seeking for answer on web I realized it's a bit complicated.
I have a page with a list of documents to download, let's call it index.html. Once a user click on a "download button", a form appears and the user's gonna be requested to provide its email. I have a proxy server file, let's name it proxy.php, which receive an email and a document id, does some things (store the email, read document in a buffer and then provide a http-response with it) and then response with a server status and, eventually, a content.
If I use a html-form to do this, it works perfectly when nothing is wrong, but I can't intercept any exception in case of error
Instead, if I use an ajax request, I can manage status codes, but I find my self with my document stored in a javascript variable and I don't know how to use it.
Thanks

You can try looking at this thread here: POST to server, receive PDF, deliver to user w/ jQuery
Assuming your document is pdf-based (though other types should be fine), you can try what the accepted answer has provided:
On the server side, add the HTTP header:
Content-Disposition: attachment; filename="whatever.pdf"

Related

How to force CSV download when it reaches the client-side? (PHP)

I am trying to serve up a CSV file as the response to a get request.
If I go onto Dev tools -> Network I can see the CSV values in the response preview, and if I double click that it will download the file. However I can't seem to get it to download automatically.
I have messed around with every type of header I can think of.
Any ideas?
Yes, It seems you can't get file downloads from ajax requests (Thanks to Quentin for the linked answer in the comments), at least not easily anyway.
This is because there is a subtle separation between the user and the browser. Ajax is triggered by the browser so the file is returned to the browser not the user.
A form submit on the other hand is triggered by the user and so the file is returned to the user (automatically downloading).
So in order to fix this I changed to a form with a post method with hidden inputs and it now works a treat.
Thanks all.

Can a HTTP PUT request specify additional information as well as the file data?

I wish to send some small files (crash log files) from my application to a web server. libCurl or WinINet seem the most obvious options, to send a simple HTTP PUT request. However is it possible to inform the server any more information about the file, like the file-name or user name, or does a PUT request by definition simply send the file contents and nothing else?
POST allows much more information but also seems far more complex, having to send content types and fake form submission, etc... if POST is the only option to do what I want what would minimal code to send a file look like, and what would my server code look like to receive it?

Sending info through API

I don't know the proper way to say this, so here is my issue.
I am dealing with an API that sends information to the server, from either web form or whatever in this format:
http://server/non_agent_api.php?source=test&user=6666&pass=1234....
with a bunch more parameters.
Normally, I have dealt with API's that just send it with SOAP or REST, not in a URL like that. My question is how do I send that information using php or something. So if I wanted to take in a username and password from a webform, how do I send that link to the server without clicking on the URL itself.
I hope that makes sense. Thanks for the help.
You can use curl for that like,
Read curl(), here you can find a class which can be easily used.
In PHP use
header("Location: TARGETURL");
Create the TARGETURL using the information sent from the form.
The Location Header makes the server to generate 302 Moved temporarily HTTP return code. The browser then sends the user to the TARGETURL transparently without any further interaction.

Retrieve file from url with autorization PHP

I'm currently trying to grab a file from an external url that has an authorization box that pops up (like the default one asking for a username and password)
How can I have a script get the contents of the page (it's a video), save it to a directory and handle the authorization (i have a username and password)
Thanks :)
file_put_contents('where to put it', file_get_contents('http://username:password#domain.com/video'));
In a word, look at curl: http://php.net/curl, for all you posting/logging in/cookies/session needs in HTTP country.
You don't need to download the page, just check what is being submitted to the web server. Chances are it's just a POST. It may have some additional checks (i.e. checksum) which may need to be scraped from the page.
You can use the HTTP Headers plugin for Firefox to see how the browser is communicating with the server. You then just need to emulate that transaction. It is likely a POST, which is easy to do with CURL.
I don't think file_put_contents will work since it doesn't do an http POST.

sending and receiving files with PHP jQuery

How can I send and receive files live like in yahoo chat? i.e if I send the file than the user on the other end will only get that file if he click on the accept button and if he click deny than the file should not upload and should be deleted from the server... I want to do that via PHP jQuery $.ajax().
This is a rather complicated question, and not easily addressed. Here are a couple things:
You'll want a way to identify conversations. You wouldn't want your file-request getting intercepted by a completely different set of people.
You'll probably want to invoke some form of long-polling that will send a request to the server, who in turn will send a request (link) to the other user, who, when clicks the link, the server will return the first request back to you, initializing your upload.
You can use the jQuery Plugin Uploadify for the asynchronous uploads, and late-initialization from a server-response.
You may also want to keep a database table to list all of the file shared by any particular conversation. This would be queries to find out if new files need to be listed in the users windows.

Categories