How can I upload files asynchronously on ASP.NET? - c#

I've been looking for a way to achieve this behavior and I found this sample project.
The trick in this project is that it changes the form target to an iframe created on the fly.
So far so good, I can get the byte[] on the server-side. But I need to change an image preview after the file is uploaded.
How can I get the iframe to update the main page? Would I have to save it on a file on the disk, make a javascript callback to change the image url? Is there another way to do this? What's recommended?
This control suggested by vorrtex actually causes a full page postback, am I missing something or is it the correct behavior?

I recommend you to use AsyncFileUploader and UpdatePanel. You have to save file on the disk but you can use C# for changing imageUrl.

Add a <script> block to the page that gets loaded in the <iframe> that interacts with the parent page and updates whatever you need to.

You can use the project mentioned below to preview the image before uploading. Working sample is also attached.
http://www.dotnetspider.com/resources/40858-Preview-Image-before-uploading-using.aspx
This uses AjaxControlToolKit's AsyncFileUploadControl and HTTPHandler to upload the image.

Related

File upload that looks like ajax

I am trying to create a form which allow async file uploading with asp.net. I realize you cannot upload a file with ajax per se so I am examining alternatives
What is the best way to do this? Create an Iframe on the page with the entire form including the file input? Can I on the parent to the frame have the submit button which forces the frame to submit and then displays some sort of spinner to indicate file is uploading? Ideally upon completion I'd like to redirect the user to another page. Is there a somewhat easy way to do this???
Have you tried using one of the jquery plugins vice doing it by hand?
http://aquantum-demo.appspot.com/file-upload
Why not use the ASP.NET AJAX Control Toolkit's AsyncFileUpload control? It's free and works pretty well.
You could use http://jquery.malsup.com/form/#file-upload
Have it post to a page that will handle a file upload on the server side in your usual way.
I like to have the page return JSON with a success/failure flag and message, then parse the response to determine if the upload succeeded.

File Upload control and Ajax AsyncFileUpload control

Can any one tell me what is the difference between both of them and also why File Upload control doesn't work with Update Panel?
It's not supported by web browsers.
Why don't file uploads work during async postbacks?
Please have a look at this thread.

how to download the image which will be displayed after a cache image on the web page?

I want to download an image from a cartoon website. and my app is WinForm,not WebForm.
So let's say that there is an image on the a.html.
Normally, when I click the previous page and am redirected to this page,
there will be a image :"image is loading",let's say A.jpg, in the same block.
After 5 seconds, the real one,let's say B.jpg, will be displayed.
So what I got is only the caching image rather than the one,B.jpg, which I want.
So..... how should I do it?
Thanks in advance.
ps: I have posted this qustion for more than 48 hours, and only got a few of answers which don't solve my problem.
I am wondering that why there are only 2 people posted their answers?
Is my question not clear?
If any, please let me know.
Thanks
EDIT: Original answer removed since I misunderstood the question entirely.
What you want to do is basically HTML scraping: using the actual html of the page to discover where files are hosted and download them. Because I'm not sure if there are any legal reasons that would prevent you from downloading the image files in this manner, I'm just going to outline an approach to doing this and not provide any working samples or anything. In other words, use this information at your own risk.
Using fiddler2 in Firefox, you should be able to find the domain and full url that one of the images is downloaded from. Basically just start fiddler2, navigate to the site in firefox, and then look for the biggest file that is downloaded. That will tell you exactly where the image is coming from.
Next, take a look at the HTML source code for the page you are viewing. The way this particular site works, it looks like it hides the previous/next downloads in a swf or something, but you can find the urls in the javascript for the page. Look for a javascript array called picArr.
To download these using a WinForms app, I would use the WebRequest object. Create a request for each image url and save the response to disk.

C# Winforms WebBrowser edit html

I want to display a webpage in a WebBroswer in my winforms application. I however want to use some custom css to change how the page looks. Is it possible to attach a style sheet and edit the html page you are viewing?
You can use this web control http://www.codeproject.com/KB/miscctrl/csEXWB.aspx
and save it as a web page and load and you can show the source in another text box , once changes are made you can save to a temp file and load it. There is an option to view local file.
I don't think that kind of thing is supported.. but anyway that would be a horrible thing to do. you may end-up interfering with the CSS used by the client page.
Have you ever seen your browser override your CSS used by page and say i want to show it this way. (I know development tools like firebug,etc do it)

C# code for saving an entire web page? (with images/formatting)

I've been struggling to find an exmample of some C# code (I'm using C# Visual Studio 2008 Express) that can programmatically save an entire web page (given a URL) including the images and formatting (e.g. CSS). The intention is that in a subsequent phase I'd ship this off (not sure how yet) so it could be viewed later via a browser.
Is there an example of the most simple approach (leveraging the .NET Framework methods) to save an entire web page? Saving as one page with a subdirectory for images, or otherwise. Basically the same as what you get with browsers when you say "save entire web page".
The simplest way is probably to add a WebBrowser Control to your application and point it at the page you want to save using the Navigate() method.
Then, when the document has loaded, call the ShowSaveAsDialog method. The user can then save the page as a single file, or a file with images in a subdirectory.
[Update]
Having now noticed "programatically" in your question, the above approach is not ideal as it requires either user involvement or delving into the Windows API to send input using SendKeys or similar.
There is nothing built-in to the .NET Framework that does all of what you ask.
So my approach revised would be:
Use System.NET.HttpWebRequest to get the main HTML document as a string or stream (easy).
Load this into a HTMLAgilityPack document where you can now easily query the document to get lists of all image elements, stylesheet links, etc.
Then make a separate web request for each of these files and save them to a subdirectory.
Finally update all relevent links in the main page to point to the items in the subdirectory.
In effect you would be implementing a very simple web browser. You may run into issues with pages that use JavaScript to dynamically alter or request page content, but for most pages this should give acceptable results.
From code Project: ZetaWebSpider
It's definitely not elegant, but you could navigate a System.Windows.Forms.WebBrowser to the URL and then call its ShowSaveAsDiagog() method to save the page.

Categories

Resources