Display attachment from HttpWebResponse

H

hlabbott

Hi

I'm having a problem displaying attachments correctly. I have
messages with attachments stored on Exchange2000 and want to be able
to click a hyperlink in my project like in OWA and see an attachment.
Because it is stored on Exchange I have to set a username and password
so I do a GET request. The data I get back seems to still be encoded
- trying to open an excel file for example results in it trying to
open it but an excel message appearing to say the file is not in a
recognizable format but that I can go ahead and view it anyway - when
I do it is the correct text but with loads of squares as well instead
of formatting but at least I know I am getting the correct attachment
back.

Is there another Response setting I should be using or is it how I'm
doing the reading from the stream that is the problem? I've tried a
BinaryReader and a StreamReader; content-type set to the response
content type and to "application/octet-stream"; disposition set to
attachment and inline. Knowing my luck it's the one combination of
the above that I haven't tried! Any suggestions even if just to
suggest a different group to post to?

Thanks

Code:

I always get the HttpWebResponse the same way:

//gets the webresponse with the attachment data
private HttpWebResponse getResponse(string url, string username,
string password, string domain)
{
HttpWebRequest request;

//get the raw data
request = (HttpWebRequest)WebRequest.Create(url);
NetworkCredential myCred = new NetworkCredential(username, password,
domain );
request.Credentials = myCred;
request.Headers.Add("translate: f");
return (HttpWebResponse)request.GetResponse();
}

//uses streamreader
private void test3(string url, string username, string password,
string domain)
{
HttpWebResponse response = getResponse(url, username, password,
domain);
StreamReader sr1;
try
{
Stream theStream = response.GetResponseStream();
sr1 = new StreamReader(theStream);
string filename = url.Substring(url.LastIndexOf("/"));
// string disposition = "attachment; filename="+filename;
string disposition = "inline; filename="+filename;
string contentType = response.Headers["Content-Type"];
Response.AppendHeader("Content-Disposition",disposition);
Response.ContentType = contentType;
Response.Write(sr1.ReadToEnd());
Response.End();
}
catch(Exception ex)
{
lblFeedback.Text = ex.ToString();
}
}

private void test2(string url, string username, string password,
string domain)
{
Response.Buffer = true;

HttpWebResponse response;
StreamReader sr1;
try
{
response = getResponse(url, username,password,domain);

Stream theStream = response.GetResponseStream();
sr1 = new StreamReader(theStream);

BinaryReader br = new BinaryReader(sr1.BaseStream);
//BinaryReader br = new BinaryReader(theStream); also tried this way

string filename = url.Substring(url.LastIndexOf("/")+1);
int buffer = 65536;

Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename="+
filename);
Response.Flush();

while(br.PeekChar() > -1)
{ //it never comes in here!
Response.BinaryWrite(br.ReadBytes(buffer));
Response.Flush();
br.BaseStream.Flush();
}
br.Close();
theStream.Close();
}
catch(Exception ex)
{
lblFeedback.Text = ex.ToString();
}
}

private void test1(string url, string username, string password,
string domain)
{
Stream istream = null;
try
{
HttpWebResponse response = response = getResponse(url,
username,password,domain);
istream = response.GetResponseStream();
int contentLength = Convert.ToInt32(response.ContentLength);
byte[] buffer = new byte[contentLength];
// Read and write data
istream.Read(buffer, 0, contentLength);
Response.Clear();
Response.ContentType = "application/octet-stream";
string filename = url.Substring(url.LastIndexOf("/")+1);
Response.AddHeader("Content-Disposition", "attachment; filename="+
filename);

Response.BinaryWrite(buffer);
Response.End();
}
finally
{
if (istream != null)
{
istream.Close();
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top