Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > context.Response.OutputStream.Write(buffer, 0, count) questions

Reply
Thread Tools

context.Response.OutputStream.Write(buffer, 0, count) questions

 
 
AAaron123
Guest
Posts: n/a
 
      11-13-2008
trying to understand the below shown code.
After this is run the browser opens a file-save dialog box for saving the
file.
I wonder how it knows I want the file saved?

But more important, the dialog box has as the default file name the name of
this ashx file.

I'd like to make that name more meaningfull but don't know how it gets set.
I'm looking for a Response.Write but don't find one. Maybe that why the
funny default filename?

Got a clue as to how that gets set?

Also, I see the content.Request below. Looks like a chance for the user to
request different size images. But I don't see anything happening when I run
the code!

Do you know what that code should be accomplishing?

I read the help for IHttpHandler ProcessRequest but that didn't help me

Thanks for any help at all

Sub ProcessRequest(ByVal context As HttpContext) Implements
IHttpHandler.ProcessRequest

' Set up the response settings

context.Response.ContentType = "image/jpeg"

context.Response.Cache.SetCacheability(HttpCacheab ility.Public)

context.Response.BufferOutput = False

' Setup the Size Parameter

Dim size As PhotoSize = PhotoSize.Original

Select Case context.Request.QueryString("Size")

Case "S"

size = PhotoSize.Small

Case "M"

size = PhotoSize.Medium

Case "L"

size = PhotoSize.Large

Case Else

size = PhotoSize.Original

End Select

' Setup the PhotoID Parameter

Dim id As Int32 = 1

Dim stream As IO.Stream = Nothing

If ((Not (context.Request.QueryString("PhotoID")) Is Nothing) _

AndAlso (context.Request.QueryString("PhotoID") <> "")) Then

id = [Convert].ToInt32(context.Request.QueryString("PhotoID"))

stream = PhotoManager.GetPhoto(id, size)

Else

id = [Convert].ToInt32(context.Request.QueryString("AlbumID"))

stream = PhotoManager.GetFirstPhoto(id, size)

End If

' Get the photo from the database, if nothing is returned, get the default
"placeholder" photo

If (stream Is Nothing) Then

stream = PhotoManager.GetPhoto(size)

End If

' Write image stream to the response stream

Dim buffersize As Integer = (1024 * 16)

Dim buffer() As Byte = New Byte((buffersize) - 1) {}

Dim count As Integer = stream.Read(buffer, 0, buffersize)


Do While (count > 0)

context.Response.OutputStream.Write(buffer, 0, count)

count = stream.Read(buffer, 0, buffersize)


Loop

End Sub


 
Reply With Quote
 
 
 
 
George
Guest
Posts: n/a
 
      11-13-2008
Here you go
http://support.microsoft.com/kb/260519/EN-US/

Basically this is the line you need to add
Response.AddHeader "content-disposition","attachment; filename=fname.ext"


where fname.ext is the file name you want it to be....

George.


"AAaron123" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> trying to understand the below shown code.
> After this is run the browser opens a file-save dialog box for saving the
> file.
> I wonder how it knows I want the file saved?
>
> But more important, the dialog box has as the default file name the name
> of this ashx file.
>
> I'd like to make that name more meaningfull but don't know how it gets
> set. I'm looking for a Response.Write but don't find one. Maybe that why
> the funny default filename?
>
> Got a clue as to how that gets set?
>
> Also, I see the content.Request below. Looks like a chance for the user to
> request different size images. But I don't see anything happening when I
> run the code!
>
> Do you know what that code should be accomplishing?
>
> I read the help for IHttpHandler ProcessRequest but that didn't help me
>
> Thanks for any help at all
>
> Sub ProcessRequest(ByVal context As HttpContext) Implements
> IHttpHandler.ProcessRequest
>
> ' Set up the response settings
>
> context.Response.ContentType = "image/jpeg"
>
> context.Response.Cache.SetCacheability(HttpCacheab ility.Public)
>
> context.Response.BufferOutput = False
>
> ' Setup the Size Parameter
>
> Dim size As PhotoSize = PhotoSize.Original
>
> Select Case context.Request.QueryString("Size")
>
> Case "S"
>
> size = PhotoSize.Small
>
> Case "M"
>
> size = PhotoSize.Medium
>
> Case "L"
>
> size = PhotoSize.Large
>
> Case Else
>
> size = PhotoSize.Original
>
> End Select
>
> ' Setup the PhotoID Parameter
>
> Dim id As Int32 = 1
>
> Dim stream As IO.Stream = Nothing
>
> If ((Not (context.Request.QueryString("PhotoID")) Is Nothing) _
>
> AndAlso (context.Request.QueryString("PhotoID") <> "")) Then
>
> id = [Convert].ToInt32(context.Request.QueryString("PhotoID"))
>
> stream = PhotoManager.GetPhoto(id, size)
>
> Else
>
> id = [Convert].ToInt32(context.Request.QueryString("AlbumID"))
>
> stream = PhotoManager.GetFirstPhoto(id, size)
>
> End If
>
> ' Get the photo from the database, if nothing is returned, get the default
> "placeholder" photo
>
> If (stream Is Nothing) Then
>
> stream = PhotoManager.GetPhoto(size)
>
> End If
>
> ' Write image stream to the response stream
>
> Dim buffersize As Integer = (1024 * 16)
>
> Dim buffer() As Byte = New Byte((buffersize) - 1) {}
>
> Dim count As Integer = stream.Read(buffer, 0, buffersize)
>
>
> Do While (count > 0)
>
> context.Response.OutputStream.Write(buffer, 0, count)
>
> count = stream.Read(buffer, 0, buffersize)
>
>
> Loop
>
> End Sub
>
>


 
Reply With Quote
 
 
 
 
AAaron123
Guest
Posts: n/a
 
      11-13-2008
That's interesting, because the code did not contain that statement, as the
site suggested it should.
And it worked except for the crazy filename in the dialog box.
Do you think the context.Response.ContentType = "image/jpeg" line caused the
dialog box to open?

I see in the code lines like:
zz=context.Request.QueryString("Size")

So, just to see, I tried:
Dim s As String = context.Request.QueryString("Filename")

which of course doesn't work.

Am I close?

Is there something that does work to get the filename?



Thanks for the info



"George" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Here you go
> http://support.microsoft.com/kb/260519/EN-US/
>
> Basically this is the line you need to add
> Response.AddHeader "content-disposition","attachment; filename=fname.ext"
>
>
> where fname.ext is the file name you want it to be....
>
> George.
>
>
> "AAaron123" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> trying to understand the below shown code.
>> After this is run the browser opens a file-save dialog box for saving the
>> file.
>> I wonder how it knows I want the file saved?
>>
>> But more important, the dialog box has as the default file name the name
>> of this ashx file.
>>
>> I'd like to make that name more meaningfull but don't know how it gets
>> set. I'm looking for a Response.Write but don't find one. Maybe that why
>> the funny default filename?
>>
>> Got a clue as to how that gets set?
>>
>> Also, I see the content.Request below. Looks like a chance for the user
>> to request different size images. But I don't see anything happening when
>> I run the code!
>>
>> Do you know what that code should be accomplishing?
>>
>> I read the help for IHttpHandler ProcessRequest but that didn't help me
>>
>> Thanks for any help at all
>>
>> Sub ProcessRequest(ByVal context As HttpContext) Implements
>> IHttpHandler.ProcessRequest
>>
>> ' Set up the response settings
>>
>> context.Response.ContentType = "image/jpeg"
>>
>> context.Response.Cache.SetCacheability(HttpCacheab ility.Public)
>>
>> context.Response.BufferOutput = False
>>
>> ' Setup the Size Parameter
>>
>> Dim size As PhotoSize = PhotoSize.Original
>>
>> Select Case context.Request.QueryString("Size")
>>
>> Case "S"
>>
>> size = PhotoSize.Small
>>
>> Case "M"
>>
>> size = PhotoSize.Medium
>>
>> Case "L"
>>
>> size = PhotoSize.Large
>>
>> Case Else
>>
>> size = PhotoSize.Original
>>
>> End Select
>>
>> ' Setup the PhotoID Parameter
>>
>> Dim id As Int32 = 1
>>
>> Dim stream As IO.Stream = Nothing
>>
>> If ((Not (context.Request.QueryString("PhotoID")) Is Nothing) _
>>
>> AndAlso (context.Request.QueryString("PhotoID") <> "")) Then
>>
>> id = [Convert].ToInt32(context.Request.QueryString("PhotoID"))
>>
>> stream = PhotoManager.GetPhoto(id, size)
>>
>> Else
>>
>> id = [Convert].ToInt32(context.Request.QueryString("AlbumID"))
>>
>> stream = PhotoManager.GetFirstPhoto(id, size)
>>
>> End If
>>
>> ' Get the photo from the database, if nothing is returned, get the
>> default "placeholder" photo
>>
>> If (stream Is Nothing) Then
>>
>> stream = PhotoManager.GetPhoto(size)
>>
>> End If
>>
>> ' Write image stream to the response stream
>>
>> Dim buffersize As Integer = (1024 * 16)
>>
>> Dim buffer() As Byte = New Byte((buffersize) - 1) {}
>>
>> Dim count As Integer = stream.Read(buffer, 0, buffersize)
>>
>>
>> Do While (count > 0)
>>
>> context.Response.OutputStream.Write(buffer, 0, count)
>>
>> count = stream.Read(buffer, 0, buffersize)
>>
>>
>> Loop
>>
>> End Sub
>>
>>

>



 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      11-13-2008
you need to study the http protocol more closely. a web response has headers
and content. for what you are doing there are two header. "content-type"
which defines the what is being returnd (in your case a jpeg).

the default file name in save dialog for a browser is the name of the
request. you can alter this with the "content-disposition" header.

Response.AddHeader("content-disposition","attachment; filename=myImg.jpg");



-- bruce (sqlwork.com)


"AAaron123" wrote:

> trying to understand the below shown code.
> After this is run the browser opens a file-save dialog box for saving the
> file.
> I wonder how it knows I want the file saved?
>
> But more important, the dialog box has as the default file name the name of
> this ashx file.
>
> I'd like to make that name more meaningfull but don't know how it gets set.
> I'm looking for a Response.Write but don't find one. Maybe that why the
> funny default filename?
>
> Got a clue as to how that gets set?
>
> Also, I see the content.Request below. Looks like a chance for the user to
> request different size images. But I don't see anything happening when I run
> the code!
>
> Do you know what that code should be accomplishing?
>
> I read the help for IHttpHandler ProcessRequest but that didn't help me
>
> Thanks for any help at all
>
> Sub ProcessRequest(ByVal context As HttpContext) Implements
> IHttpHandler.ProcessRequest
>
> ' Set up the response settings
>
> context.Response.ContentType = "image/jpeg"
>
> context.Response.Cache.SetCacheability(HttpCacheab ility.Public)
>
> context.Response.BufferOutput = False
>
> ' Setup the Size Parameter
>
> Dim size As PhotoSize = PhotoSize.Original
>
> Select Case context.Request.QueryString("Size")
>
> Case "S"
>
> size = PhotoSize.Small
>
> Case "M"
>
> size = PhotoSize.Medium
>
> Case "L"
>
> size = PhotoSize.Large
>
> Case Else
>
> size = PhotoSize.Original
>
> End Select
>
> ' Setup the PhotoID Parameter
>
> Dim id As Int32 = 1
>
> Dim stream As IO.Stream = Nothing
>
> If ((Not (context.Request.QueryString("PhotoID")) Is Nothing) _
>
> AndAlso (context.Request.QueryString("PhotoID") <> "")) Then
>
> id = [Convert].ToInt32(context.Request.QueryString("PhotoID"))
>
> stream = PhotoManager.GetPhoto(id, size)
>
> Else
>
> id = [Convert].ToInt32(context.Request.QueryString("AlbumID"))
>
> stream = PhotoManager.GetFirstPhoto(id, size)
>
> End If
>
> ' Get the photo from the database, if nothing is returned, get the default
> "placeholder" photo
>
> If (stream Is Nothing) Then
>
> stream = PhotoManager.GetPhoto(size)
>
> End If
>
> ' Write image stream to the response stream
>
> Dim buffersize As Integer = (1024 * 16)
>
> Dim buffer() As Byte = New Byte((buffersize) - 1) {}
>
> Dim count As Integer = stream.Read(buffer, 0, buffersize)
>
>
> Do While (count > 0)
>
> context.Response.OutputStream.Write(buffer, 0, count)
>
> count = stream.Read(buffer, 0, buffersize)
>
>
> Loop
>
> End Sub
>
>
>

 
Reply With Quote
 
AAaron123
Guest
Posts: n/a
 
      11-13-2008

"bruce barker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> you need to study the http protocol more closely. a web response has
> headers


That suggestion was helpful. I googled "http protocol", read it, then read
about QueryString and ran a little code and it looks like the request
contains only an image number and the size. So in your future replies please
continue to point people to what they should read about. Now I have to find
out what caused the request and what determins what is included. I think I
know where to look.

thanks


> and content. for what you are doing there are two header. "content-type"
> which defines the what is being returnd (in your case a jpeg).
>
> the default file name in save dialog for a browser is the name of the
> request. you can alter this with the "content-disposition" header.
>
> Response.AddHeader("content-disposition","attachment;
> filename=myImg.jpg");
>
>
>
> -- bruce (sqlwork.com)
>
>
> "AAaron123" wrote:
>
>> trying to understand the below shown code.
>> After this is run the browser opens a file-save dialog box for saving the
>> file.
>> I wonder how it knows I want the file saved?
>>
>> But more important, the dialog box has as the default file name the name
>> of
>> this ashx file.
>>
>> I'd like to make that name more meaningfull but don't know how it gets
>> set.
>> I'm looking for a Response.Write but don't find one. Maybe that why the
>> funny default filename?
>>
>> Got a clue as to how that gets set?
>>
>> Also, I see the content.Request below. Looks like a chance for the user
>> to
>> request different size images. But I don't see anything happening when I
>> run
>> the code!
>>
>> Do you know what that code should be accomplishing?
>>
>> I read the help for IHttpHandler ProcessRequest but that didn't help me
>>
>> Thanks for any help at all
>>
>> Sub ProcessRequest(ByVal context As HttpContext) Implements
>> IHttpHandler.ProcessRequest
>>
>> ' Set up the response settings
>>
>> context.Response.ContentType = "image/jpeg"
>>
>> context.Response.Cache.SetCacheability(HttpCacheab ility.Public)
>>
>> context.Response.BufferOutput = False
>>
>> ' Setup the Size Parameter
>>
>> Dim size As PhotoSize = PhotoSize.Original
>>
>> Select Case context.Request.QueryString("Size")
>>
>> Case "S"
>>
>> size = PhotoSize.Small
>>
>> Case "M"
>>
>> size = PhotoSize.Medium
>>
>> Case "L"
>>
>> size = PhotoSize.Large
>>
>> Case Else
>>
>> size = PhotoSize.Original
>>
>> End Select
>>
>> ' Setup the PhotoID Parameter
>>
>> Dim id As Int32 = 1
>>
>> Dim stream As IO.Stream = Nothing
>>
>> If ((Not (context.Request.QueryString("PhotoID")) Is Nothing) _
>>
>> AndAlso (context.Request.QueryString("PhotoID") <> "")) Then
>>
>> id = [Convert].ToInt32(context.Request.QueryString("PhotoID"))
>>
>> stream = PhotoManager.GetPhoto(id, size)
>>
>> Else
>>
>> id = [Convert].ToInt32(context.Request.QueryString("AlbumID"))
>>
>> stream = PhotoManager.GetFirstPhoto(id, size)
>>
>> End If
>>
>> ' Get the photo from the database, if nothing is returned, get the
>> default
>> "placeholder" photo
>>
>> If (stream Is Nothing) Then
>>
>> stream = PhotoManager.GetPhoto(size)
>>
>> End If
>>
>> ' Write image stream to the response stream
>>
>> Dim buffersize As Integer = (1024 * 16)
>>
>> Dim buffer() As Byte = New Byte((buffersize) - 1) {}
>>
>> Dim count As Integer = stream.Read(buffer, 0, buffersize)
>>
>>
>> Do While (count > 0)
>>
>> context.Response.OutputStream.Write(buffer, 0, count)
>>
>> count = stream.Read(buffer, 0, buffersize)
>>
>>
>> Loop
>>
>> End Sub
>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Few Questions (HW questions already answered by me) padh.ayo@gmail.com C Programming 10 12-06-2006 05:48 PM
Malloc and free questions - learner questions pkirk25 C Programming 50 10-04-2006 02:22 PM
Questions on Canon 300D and etc. questions regarding digital photography Progressiveabsolution Digital Photography 12 03-24-2005 05:18 PM
Newbie questions - Couple of VC++ questions regarding dlls and VB6 Ali Syed C Programming 3 10-13-2004 10:15 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM



Advertisments