473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Download File From Content-Disposition

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().GetResponseS tream());
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", Rece

Aug 1 '06 #1
4 2480
See http://www.ietf.org/rfc/rfc1806.txt

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Magician

A man, a plan, a canal.
a palindrome that has gone to s**t.
"rony_16" <ro***********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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().GetResponseS tream());
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", Rece

Aug 1 '06 #2
Ok , But How to i read the file in C# ?
Kevin Spencer wrote:
See http://www.ietf.org/rfc/rfc1806.txt

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Magician

A man, a plan, a canal.
a palindrome that has gone to s**t.
"rony_16" <ro***********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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().GetResponseS tream());
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", Rece
Aug 1 '06 #3
You should be able to get the file via the HttpWebResponse.GetResponseStream
method.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Magician

A man, a plan, a canal.
a palindrome that has gone to s**t.
"rony_16" <ro***********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Ok , But How to i read the file in C# ?
Kevin Spencer wrote:
>See http://www.ietf.org/rfc/rfc1806.txt

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Magician

A man, a plan, a canal.
a palindrome that has gone to s**t.
"rony_16" <ro***********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googleg roups.com...
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().GetResponseS tream());
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", Rece

Aug 1 '06 #4
But i don't get the stream .
i think it is because of the "Content-Disposition:attachment;
filename=changehistory.csv" header .
but i can't find any information about , "how to get this file from the
header" .
Kevin Spencer wrote:
You should be able to get the file via the HttpWebResponse.GetResponseStream
method.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Magician

A man, a plan, a canal.
a palindrome that has gone to s**t.
"rony_16" <ro***********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Ok , But How to i read the file in C# ?
Kevin Spencer wrote:
See http://www.ietf.org/rfc/rfc1806.txt

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Magician

A man, a plan, a canal.
a palindrome that has gone to s**t.
"rony_16" <ro***********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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().GetResponseS tream());
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", Rece
Aug 2 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: BOHICA | last post by:
I would like on my page when users click one of my mp3 files for the dialog box to pop up so they can choose to save the file to disk instead of trying to open through IE, but the dang thing won't...
6
by: Topmind | last post by:
If the Windows Web browser (IE 5 or 6) opens an XLS file (spreadsheet), how it handles it depends on a file association setting within Windows. There is a setting called "confirm open after...
8
by: lawrence | last post by:
Under the domain publicpen.com I've several dozen sites in subdiretories, such as www.publicpen.com/honenbeger. I've no trouble with any of these sites. But under one, which I put in yesterday,...
6
by: Bryan Martin | last post by:
Ok im trying to figure out how internet explorers file download box always seem to at least come close to knowing the exact file size/time to download. From what I can tell its getting its not...
0
by: Sarah Akers | last post by:
GgF ----gL5cJ72EqiGIQ0SK65Rz Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <head> <style type=3D"text/css">.eyebrow { FONT-WEIGHT: bold; FONT-SIZE: 10px; TE=
0
by: Anne Snow | last post by:
7qs ----bSCMEc0pLIbJxmV9 Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html><head><style type=3Dtext/css>.eyebrow { FONT-WEIGHT: bold; FONT-SIZE= : 10px;...
5
by: Tim_Mac | last post by:
hi, i read that by adding the following code to by aspx pages, it would not store temporary internet files: Response.Cache.SetCacheability(HttpCacheability.NoCache); it didn't actually work...
5
by: Neil Rossi | last post by:
I have an issue with a particular ASP page on two web servers. Let's call these servers Dev1 and Beta1. Both Servers are running IIS 5, Windows 2000 SP4 with "almost" all of the latest patches. ...
3
by: rafi | last post by:
Hi, I developed a website using php5 & mysql5. One of its features is the ability of users to upload files to the server, and allow other users to view these files. I allow uploading the...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.