tags:

views:

73

answers:

4

I have a stream object in which I have some document, I know what document is it, how can I open that document without saving it into a file physically. So I want to directlly open the document from stream itself without saving it.

How to do that? I doing it in c#

A: 

You can use MemoryStream to use the document only in RAM.

Nayan
A: 

Unfortunately most programs (Word, Excel, Notepad, ...) need a filename to open a file. So i think there is no real solution to your problem.

The only think would be to write a File System Filter Driver, that returns the MemoryStream for a virtual filename, but that driver has to be written in C++ and is not the easiest one to do.

Oliver
A: 

this is how i do such a thing, but i know the file's name. this is method is called into an empty aspx page (i mean without any kind of html markup except the <@Page ... /> line )

private void LoadAttachment()
    {
            byte[] ImageData = ... get data from somewhere ...

            Response.Buffer = true;
            String filename = "itakethis.fromdatabase";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
            Response.ContentType = GetMimeType(filename);//you can try to extrapolate it from file extension
            if(ImageData.Length > 0)
                Response.BinaryWrite(ImageData);
            else
                Response.BinaryWrite(new byte[1]);
            Response.Flush();
            ApplicationInstance.CompleteRequest();
    }

`

Stefano
@Stefano, there's some useful stuff there, but the OP is asking about `Stream` not `byte[]`, and since you check for `.Length` your solution will not translate.
Kirk Woll
@Kirk Woll, sorry, my mistake, i thougth the problem was to physically save the file, not to directly use the stream. Anyway, streams have a length property. If the problem is to directly use the stream, then the answer is no : bynarywrite take a byte array as argument
Stefano
A: 

Why can't you save the file?

I've used GetTempFilename to good effect in the past. I can't remember exactly which class it's in, probably System.Environment or File itself. Anyway, you can be sure the file will be in the system defined temp folder, so you can safely delete the file once you're done with it.

Antony Scott