Is there anyway to stream an automated file without saving to diskfirst?

K

Karl

Hi all,

It may seem like a rather odd request (or not) but I would like to be
able to create a file (doc, jpg, xls or one of many other files that
can be automated) on a website and stream it to a user without saving
it to the disk first.

Obviously saving the file first and streaming it afterwards is fairly
easy to do, but it relies on disk write permissions on the server and
you'd have to make sure the file was removed afterwards.

I'd like to be able to dynamically create a file and stream this to
the user. Can anyone tell me if this is possible? If so, samples,
examples and weblinks to such would be appreciated...
 
M

Morten Wennevik [C# MVP]

Hi Karl,

You can use a MemoryStream to hold the data, and have the MemoryStream feed the webresponse
 
H

Harlan Messinger

Karl said:
Hi all,

It may seem like a rather odd request (or not) but I would like to be
able to create a file (doc, jpg, xls or one of many other files that
can be automated) on a website and stream it to a user without saving
it to the disk first.

Obviously saving the file first and streaming it afterwards is fairly
easy to do, but it relies on disk write permissions on the server and
you'd have to make sure the file was removed afterwards.

I'd like to be able to dynamically create a file and stream this to
the user. Can anyone tell me if this is possible? If so, samples,
examples and weblinks to such would be appreciated...
Why do you need an intermediary stage at all? Why not just write
directly to Response?
 
K

Karl

Hi Karl,

You can use a MemoryStream to hold the data, and have the MemoryStream feed the webresponse

This sounds like a great way to do exactly what I want, but I've had a
couple of attempts at creating an Excel spreadsheet by automation and
saving it to the MemoryStream, but with no success. I've attached some
simple code here to show what I'm trying to do. Perhaps someone could
tweak it so that it ends up with a successfull stream which I can
output to a browser?


Dim memStream As New MemoryStream(1000000)

Dim Excel As New Application
Dim WB As Workbook = Excel.Workbooks.Add()
Dim WS As Worksheet = WB.Worksheets("Sheet1")
Dim numX As Integer = 1

WS.Range("a" & numX).Value = "ColumnA"
WS.Range("b" & numX).Value = "ColumnB"
WS.Range("c" & numX).Value = "ColumnC"
WS.Range("d" & numX).Value = "ColumnD"
WS.Range("e" & numX).Value = "ColumnE"
WS.Range("a" & numX).Font.Bold = True
WS.Range("b" & numX).Font.Bold = True
WS.Range("c" & numX).Font.Bold = True
WS.Range("d" & numX).Font.Bold = True
WS.Range("e" & numX).Font.Bold = True
For numX = 2 To 10
WS.Range("a" & numX).Value = "A" & numX.ToString()
WS.Range("b" & numX).Value = "B" & numX.ToString()
WS.Range("c" & numX).Value = "C" & numX.ToString()
WS.Range("d" & numX).Value = "D" & numX.ToString()
WS.Range("e" & numX).Value = "E" & numX.ToString()
Next

WB.SaveAs(memStream, XlFileFormat.xlWorkbookNormal)
WB.Close()
Excel.Workbooks.Close()
Excel.Quit()
Excel = Nothing
GC.Collect()
 
K

Karl

Why do you need an intermediary stage at all? Why not just write
directly to Response?

Simply because I don't know how Id write a file directly to the
response object. I know how to create individual objects (jpg , Excel
etc) but I only know how to save these to the file system afterwards.
This is what I'm trying to avoid and to send the file out to a users
browser directly without saving to disk.
 
K

Karl

This sounds like a great way to do exactly what I want, but I've had a
couple of attempts at creating an Excel spreadsheet by automation and
saving it to the MemoryStream, but with no success. I've attached some
simple code here to show what I'm trying to do. Perhaps someone could
tweak it so that it ends up with a successfull stream which I can
output to a browser?

Dim memStream As New MemoryStream(1000000)

Dim Excel As New Application
Dim WB As Workbook = Excel.Workbooks.Add()
Dim WS As Worksheet = WB.Worksheets("Sheet1")
Dim numX As Integer = 1

WS.Range("a" & numX).Value = "ColumnA"
WS.Range("b" & numX).Value = "ColumnB"
WS.Range("c" & numX).Value = "ColumnC"
WS.Range("d" & numX).Value = "ColumnD"
WS.Range("e" & numX).Value = "ColumnE"
WS.Range("a" & numX).Font.Bold = True
WS.Range("b" & numX).Font.Bold = True
WS.Range("c" & numX).Font.Bold = True
WS.Range("d" & numX).Font.Bold = True
WS.Range("e" & numX).Font.Bold = True
For numX = 2 To 10
WS.Range("a" & numX).Value = "A" & numX.ToString()
WS.Range("b" & numX).Value = "B" & numX.ToString()
WS.Range("c" & numX).Value = "C" & numX.ToString()
WS.Range("d" & numX).Value = "D" & numX.ToString()
WS.Range("e" & numX).Value = "E" & numX.ToString()
Next

WB.SaveAs(memStream, XlFileFormat.xlWorkbookNormal)
WB.Close()
Excel.Workbooks.Close()
Excel.Quit()
Excel = Nothing
GC.Collect()

Bump...
 
K

Karl

Simply because I don't know how Id write a file directly to the
response object. I know how to create individual objects (jpg , Excel
etc) but I only know how to save these to the file system afterwards.
This is what I'm trying to avoid and to send the file out to a users
browser directly without saving to disk.

bump...
 

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