0

Basically I wan't to save an image loaded in a webBrowser control. The only way I can get this to work at the moment is by showing the save as dialog.

is there a way to pass in a path and make it save itself? (hide the dialog I ask to show!)

is there another way to save the image? I can't seem to get documentstream to work. I have also tried webclient.filedownload(...) but get an error 302 ( "(302) Found Redirect.")

DownloadFileAsync gives no error but an empty jpeg file?

files are always jpegs, but not always at same location.

albertjan
  • 7,739
  • 6
  • 44
  • 74

1 Answers1

0

You should go with HttpWebRequest.

It has the capability to follow a 302 automatically.

var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.contoso.com"); 
//increase this number if there are more then one redirects. 
myHttpWebRequest.MaximumAutomaticRedirections = 1;
myHttpWebRequest.AllowAutoRedirect = true;
var myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();    

var buff = new byte[myHttpWebResponse.ContentLength];

// here you specify the path to the file. The path in this example is : image.jpg
// if you want to store it in the application root use:
// AppDomain.CurrentDomain.BaseDirectory + "\\image.jpg"
using (var sw = new BinaryWriter(File.Open("c:\\image.jpg", FileMode.OpenOrCreate)))
{
    using (var br = new BinaryReader (myHttpWebResponse.GetResponseStream ()))
    {
        br.Read(buff, 0, (int)myHttpWebResponse.ContentLength);
        sw.Write(buff);
    }            
}
albertjan
  • 7,739
  • 6
  • 44
  • 74
  • yeah i had read elsewhere that that should work, i tried your code and still got a 302? also where does the file get saved?(how can i designate an output path) –  Apr 12 '12 at 19:24
  • There was a little bug in it. :) Try this. Can you share the link to the image that is redirected? – albertjan Apr 12 '12 at 19:43
  • i was told i couldn't share the data. still get a redirect error in line 4. Can I block the redirect? I can't see why this is happening, when you view the profile picture in a web browser it shows fine and stays on the image, redirects nowhere? –  Apr 12 '12 at 19:56
  • if i set: myHttpWebRequest.AllowAutoRedirect = false; it goes to the next line, but is too big for the byte? –  Apr 12 '12 at 20:00
  • nope, it gets the error @line4 and stops, if you change autoredirect to false i get to the next line, hope that is a step forward. –  Apr 12 '12 at 20:07
  • the new error in line 5 is with response.contentLength, it is -1? –  Apr 12 '12 at 20:19
  • if i don't use content length and put in a large number say 5000, the image is created but is unreadable. –  Apr 12 '12 at 20:21
  • make the size of the buffer a couple megabytes large something like 5: `var buff = new byte[5 * 1024 * 1024];` Good luck time for bed. :p – albertjan Apr 12 '12 at 20:24
  • the image comes out in the size of the buffer yet unreadable, i will be back in about 30hrs+. thanks for the input so far. –  Apr 12 '12 at 20:29