Home All Groups Group Topic Archive Search About

Download File From Content-Disposition

Author
1 Aug 2006 1:14 PM
rony.vainblat
Hi,
I have a program that connects to a site With WebRequest and
WebResponse .
The response of this site is a file (csv file).
The problem is that the file do not comes as a stream , hi is a part of
the header
(Content-Disposition:attachment; filename=changehistory.csv)
i tried to download this file as a stream , but it do not work .
i triesd with WebClient , but i coudn't attache a cookieContainer to
him . (Connectiong to LogIn)
I did a search in the web an again i found nothing .

Please Help me , Rony

My Code is :
#region SaveStreamToFile
   /// <summary>
   /// Saves the stream on the HD to the path provided.
   /// </summary>
   /// <param name="filePath">Where to save the file. (includes the
file name)</param>
   /// <param name="stream">The stream contains the data to be
saved.</param>
   /// <returns>True - Saved successfully. Otherwise False.</returns>
   public  void SaveStreamToFile(string filePath, Stream stream)
   {
       // If not all data was provided return false;
       //if (string.IsNullOrEmpty(filePath) || stream == null)
       //    return false;
      // System.IO.Stream gzStream = stream; //= new
System.IO.Stream()//(stream,
System.IO.Compression.CompressionMode.Decompress);
       FileStream fs = null;
       try
       {
           byte[] inBuffer = new byte[10000];
           int bytesRead = 0;
           fs = File.Open(filePath, FileMode.Create, FileAccess.Write,
FileShare.Read);
           while ((bytesRead = stream.Read(inBuffer, 0,
inBuffer.Length)) > 0)
           {
               fs.Write(inBuffer, 0, bytesRead);
           }
           fs.Flush();
          // return true;
       }
       catch (Exception ex)
       {
           throw ex;
         //  ExceptionPolicy.HandleException(ex, "Global Policy");
           //return false;
       }
       finally
       {
           fs.Close();
       }
   }
   #endregion

   protected void Go1()
   {
       string get = "http://www.login.com?Usr=rony&Psw=vava";
       CookieContainer cookies = new CookieContainer();
       HttpWebRequest webRequest = WebRequest.Create(get) as
HttpWebRequest;
       webRequest.ContentType = "application/x-www-form-urlencoded";
       webRequest.CookieContainer = cookies;
       WebResponse myResponse = webRequest.GetResponse();
       Stream ReceiveStream = myResponse.GetResponseStream();
       Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
       StreamReader readStream = new StreamReader(ReceiveStream,
encode);

       string str = readStream.ReadToEnd();
       int pos = str.LastIndexOf("href=");
       int p = str.IndexOf("target", pos);

       string s = str.Substring(pos + 6, p - pos - 4);
       int v = s.IndexOf('"');
       string sv = s.Substring(0, v);
       sv = sv.Replace("&amp;", "&");

       readStream.Close();
       // now we can send out cookie along with a request for the
protected page
       webRequest = WebRequest.Create(sv) as HttpWebRequest;
       webRequest.CookieContainer = cookies;
       StreamReader responseReader = new
StreamReader(webRequest.GetResponse().GetResponseStream());
       string responseData = responseReader.ReadToEnd();
       responseReader.Close();

       // download with stream (not working)
       string d = "https://www.login.com?download=32";
       HttpWebRequest webRequest1 = WebRequest.Create(d) as
HttpWebRequest;
       webRequest1.ContentType = "application/x-www-form-urlencoded";
       webRequest1.CookieContainer = cookies;
       WebResponse myResponse1 = webRequest1.GetResponse();
       Stream ReceiveStream1 = myResponse1.GetResponseStream();
       string aa = myResponse1.Headers["Content-Disposition"];
       SaveStreamToFile(@"C:\file.csv", ReceiveStream1);
   }

Author
1 Aug 2006 1:49 PM
tommaso.gastaldi
check this out to see if related to your problem:

http://support.microsoft.com/kb/279667/en-us

-t

rony.vainb***@gmail.com ha scritto:

Show quoteHide quote
> Hi,
> I have a program that connects to a site With WebRequest and
> WebResponse .
> The response of this site is a file (csv file).
> The problem is that the file do not comes as a stream , hi is a part of
> the header
> (Content-Disposition:attachment; filename=changehistory.csv)
> i tried to download this file as a stream , but it do not work .
> i triesd with WebClient , but i coudn't attache a cookieContainer to
> him . (Connectiong to LogIn)
> I did a search in the web an again i found nothing .
>
> Please Help me , Rony
>
> My Code is :
>  #region SaveStreamToFile
>    /// <summary>
>    /// Saves the stream on the HD to the path provided.
>    /// </summary>
>    /// <param name="filePath">Where to save the file. (includes the
> file name)</param>
>    /// <param name="stream">The stream contains the data to be
> saved.</param>
>    /// <returns>True - Saved successfully. Otherwise False.</returns>
>    public  void SaveStreamToFile(string filePath, Stream stream)
>    {
>        // If not all data was provided return false;
>        //if (string.IsNullOrEmpty(filePath) || stream == null)
>        //    return false;
>       // System.IO.Stream gzStream = stream; //= new
> System.IO.Stream()//(stream,
> System.IO.Compression.CompressionMode.Decompress);
>        FileStream fs = null;
>        try
>        {
>            byte[] inBuffer = new byte[10000];
>            int bytesRead = 0;
>            fs = File.Open(filePath, FileMode.Create, FileAccess.Write,
> FileShare.Read);
>            while ((bytesRead = stream.Read(inBuffer, 0,
> inBuffer.Length)) > 0)
>            {
>                fs.Write(inBuffer, 0, bytesRead);
>            }
>            fs.Flush();
>           // return true;
>        }
>        catch (Exception ex)
>        {
>            throw ex;
>          //  ExceptionPolicy.HandleException(ex, "Global Policy");
>            //return false;
>        }
>        finally
>        {
>            fs.Close();
>        }
>    }
>    #endregion
>
>    protected void Go1()
>    {
>        string get = "http://www.login.com?Usr=rony&Psw=vava";
>        CookieContainer cookies = new CookieContainer();
>        HttpWebRequest webRequest = WebRequest.Create(get) as
> HttpWebRequest;
>        webRequest.ContentType = "application/x-www-form-urlencoded";
>        webRequest.CookieContainer = cookies;
>        WebResponse myResponse = webRequest.GetResponse();
>        Stream ReceiveStream = myResponse.GetResponseStream();
>        Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
>        StreamReader readStream = new StreamReader(ReceiveStream,
> encode);
>
>        string str = readStream.ReadToEnd();
>        int pos = str.LastIndexOf("href=");
>        int p = str.IndexOf("target", pos);
>
>        string s = str.Substring(pos + 6, p - pos - 4);
>        int v = s.IndexOf('"');
>        string sv = s.Substring(0, v);
>        sv = sv.Replace("&amp;", "&");
>
>        readStream.Close();
>        // now we can send out cookie along with a request for the
> protected page
>        webRequest = WebRequest.Create(sv) as HttpWebRequest;
>        webRequest.CookieContainer = cookies;
>        StreamReader responseReader = new
> StreamReader(webRequest.GetResponse().GetResponseStream());
>        string responseData = responseReader.ReadToEnd();
>        responseReader.Close();
>
>        // download with stream (not working)
>        string d = "https://www.login.com?download=32";
>        HttpWebRequest webRequest1 = WebRequest.Create(d) as
> HttpWebRequest;
>        webRequest1.ContentType = "application/x-www-form-urlencoded";
>        webRequest1.CookieContainer = cookies;
>        WebResponse myResponse1 = webRequest1.GetResponse();
>        Stream ReceiveStream1 = myResponse1.GetResponseStream();
>        string aa = myResponse1.Headers["Content-Disposition"];
>        SaveStreamToFile(@"C:\file.csv", ReceiveStream1);
>    }
Author
1 Aug 2006 4:08 PM
rony.vainblat
no it doesn't ,
thanks any way

tommaso.gasta***@uniroma1.it wrote:
Show quoteHide quote
> check this out to see if related to your problem:
>
> http://support.microsoft.com/kb/279667/en-us
>
> -t
>
> rony.vainb***@gmail.com ha scritto:
>
> > Hi,
> > I have a program that connects to a site With WebRequest and
> > WebResponse .
> > The response of this site is a file (csv file).
> > The problem is that the file do not comes as a stream , hi is a part of
> > the header
> > (Content-Disposition:attachment; filename=changehistory.csv)
> > i tried to download this file as a stream , but it do not work .
> > i triesd with WebClient , but i coudn't attache a cookieContainer to
> > him . (Connectiong to LogIn)
> > I did a search in the web an again i found nothing .
> >
> > Please Help me , Rony
> >
> > My Code is :
> >  #region SaveStreamToFile
> >    /// <summary>
> >    /// Saves the stream on the HD to the path provided.
> >    /// </summary>
> >    /// <param name="filePath">Where to save the file. (includes the
> > file name)</param>
> >    /// <param name="stream">The stream contains the data to be
> > saved.</param>
> >    /// <returns>True - Saved successfully. Otherwise False.</returns>
> >    public  void SaveStreamToFile(string filePath, Stream stream)
> >    {
> >        // If not all data was provided return false;
> >        //if (string.IsNullOrEmpty(filePath) || stream == null)
> >        //    return false;
> >       // System.IO.Stream gzStream = stream; //= new
> > System.IO.Stream()//(stream,
> > System.IO.Compression.CompressionMode.Decompress);
> >        FileStream fs = null;
> >        try
> >        {
> >            byte[] inBuffer = new byte[10000];
> >            int bytesRead = 0;
> >            fs = File.Open(filePath, FileMode.Create, FileAccess.Write,
> > FileShare.Read);
> >            while ((bytesRead = stream.Read(inBuffer, 0,
> > inBuffer.Length)) > 0)
> >            {
> >                fs.Write(inBuffer, 0, bytesRead);
> >            }
> >            fs.Flush();
> >           // return true;
> >        }
> >        catch (Exception ex)
> >        {
> >            throw ex;
> >          //  ExceptionPolicy.HandleException(ex, "Global Policy");
> >            //return false;
> >        }
> >        finally
> >        {
> >            fs.Close();
> >        }
> >    }
> >    #endregion
> >
> >    protected void Go1()
> >    {
> >        string get = "http://www.login.com?Usr=rony&Psw=vava";
> >        CookieContainer cookies = new CookieContainer();
> >        HttpWebRequest webRequest = WebRequest.Create(get) as
> > HttpWebRequest;
> >        webRequest.ContentType = "application/x-www-form-urlencoded";
> >        webRequest.CookieContainer = cookies;
> >        WebResponse myResponse = webRequest.GetResponse();
> >        Stream ReceiveStream = myResponse.GetResponseStream();
> >        Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
> >        StreamReader readStream = new StreamReader(ReceiveStream,
> > encode);
> >
> >        string str = readStream.ReadToEnd();
> >        int pos = str.LastIndexOf("href=");
> >        int p = str.IndexOf("target", pos);
> >
> >        string s = str.Substring(pos + 6, p - pos - 4);
> >        int v = s.IndexOf('"');
> >        string sv = s.Substring(0, v);
> >        sv = sv.Replace("&amp;", "&");
> >
> >        readStream.Close();
> >        // now we can send out cookie along with a request for the
> > protected page
> >        webRequest = WebRequest.Create(sv) as HttpWebRequest;
> >        webRequest.CookieContainer = cookies;
> >        StreamReader responseReader = new
> > StreamReader(webRequest.GetResponse().GetResponseStream());
> >        string responseData = responseReader.ReadToEnd();
> >        responseReader.Close();
> >
> >        // download with stream (not working)
> >        string d = "https://www.login.com?download=32";
> >        HttpWebRequest webRequest1 = WebRequest.Create(d) as
> > HttpWebRequest;
> >        webRequest1.ContentType = "application/x-www-form-urlencoded";
> >        webRequest1.CookieContainer = cookies;
> >        WebResponse myResponse1 = webRequest1.GetResponse();
> >        Stream ReceiveStream1 = myResponse1.GetResponseStream();
> >        string aa = myResponse1.Headers["Content-Disposition"];
> >        SaveStreamToFile(@"C:\file.csv", ReceiveStream1);
> >    }