I had a slightly harder problem Something like:. I have accomplished this with a hidden iframe. I use perl, not php, so will just give concept, not code solution.
Client sends Ajax request to server, causing the file content to be generated. This is saved as a temp file on the server, and the filename is returned to the client. It is also worth noting that you can directly chain multiple promise blocks.
This makes promises very powerful. The following block does the same thing as our original example, but is written in a different style:. Many developers like this style better, as it is flatter and arguably easier to read for longer promise chains — each subsequent promise comes after the previous one, rather than being inside the previous one which can get unwieldy.
The only other difference is that we've had to include a return statement in front of response. This really depends on what project you are working on. XHR has been around for a long time now and has very good cross-browser support. Fetch and Promises, on the other hand, are a more recent addition to the web platform, although they're supported well across the browser landscape, with the exception of Internet Explorer.
If you need to support older browsers, then an XHR solution might be preferable. If however you are working on a more progressive project and aren't as worried about older browsers, then Fetch could be a good choice. You should really learn both — Fetch will become more popular as Internet Explorer declines in usage IE is no longer being developed, in favor of Microsoft's new Edge browser , but you might need XHR for a while yet.
To round off the article, we'll look at a slightly more complex example that shows some more interesting uses of Fetch.
We have created a sample site called The Can Store — it's a fictional supermarket that only sells canned goods. You can find this example live on GitHub , and see the source code. By default, the site displays all the products, but you can use the form controls in the left hand column to filter them by category, or search term, or both.
There is quite a lot of complex code that deals with filtering the products by category and search terms, manipulating strings so the data displays correctly in the UI, etc. We won't discuss all of it in the article, but you can find extensive comments in the code see can-script.
The fetch function returns a promise. If this completes successfully, the function inside the first. Inside this function we run json on the response, not text , as we want to return our response as structured JSON data, not plain text.
Next, we chain another. We set this to be the value of the products variable, then run initialize products , which starts the process of displaying all the products in the user interface. To handle errors, we chain a. This tutorial shows how to make an AJAX request to download a file, and showing the download percentage completed. However when downloading binary files, the responseType property of the request object is set to blob.
Possible values of responseType property are empty string default , arraybuffer , blob , document , json , and text. For simply downloading binary files use blob as the response type. Green; Label2. Red; Label2. Fill ds ;. DataBind ;. Close ;. AddWithValue "id" , GridView1. Text ;. ExecuteReader ;. Clear ;. SetCacheability HttpCacheability. NoCache ;. BinaryWrite byte[] dr[ "data" ] ;. End ;. Next Recommended Reading.
0コメント