Solved

Downloading BLOB pdf to user machine ASP.NET

Posted on 2010-08-25
14
715 Views
Last Modified: 2012-06-21
I am writing a web page using ASP.NET and VB.Net that will allow users to download PDF's stored in BLOB format on the database.

My question is, when the user clicks the file, where can I save the file to on the user machine, and how do I clean up the files when the user is done. What I would like to do, is just have the machine prompt the user to open or save the file, and then the user can manage the files. The only way I have come across making this work, is have to specify a file location to create the file, then open the file. I am not sure if the users machine will even let me write the file. Any suggestions will be appreciated.


 Dim MyNewFileName As String
        Using connection As New SqlConnection(ConnectionString)
            connection.Open()
            Dim MyNewQuery As String
            MyNewQuery = "Select FileName from RetroWorkSheet where ModNumber = '" & Me.RadcmbInstructions.SelectedValue & "'"
Dim cmd2 As New System.Data.SqlClient.SqlCommand(MyNewQuery, connection)
 MyNewFileName = cmd2.ExecuteScalar
 Using command As New SqlCommand("select Retro_worksheet from RetroWorksheet where ModNumber = '" & Me.RadcmbInstructions.SelectedValue & "'", connection)
                Dim buffer As Byte() = DirectCast(command.ExecuteScalar(), Byte())
  Using fs As New FileStream("c:\" & MyNewFileName, FileMode.Create)
                    fs.Write(buffer, 0, buffer.Length)
 End Using
            End Using
        End Using
 Dim Proc As New System.Diagnostics.Process
        Proc.StartInfo.WorkingDirectory = "C:\"
        Proc.StartInfo.FileName = MyNewFileName
        Proc.Start()

0
Comment
Question by:phil301
  • 7
  • 7
14 Comments
 
LVL 1

Expert Comment

by:narendragummadi
ID: 33523346
The below code will work for your requirement.

In the below example i am reading the file content from upload control wherein for your requirement update as needed.
FileStream MyFileStream;
        long FileSize;
        MyFileStream = new FileStream(fuplXML.PostedFile.FileName.ToString(), FileMode.Open);
        FileSize = MyFileStream.Length;
        byte[] Buffer = new byte[(int)FileSize];
        MyFileStream.Read(Buffer, 0, (int)FileSize);
        MyFileStream.Close();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.Buffer = true;
        Response.Expires = -1;
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "inline; filename=ConversionResult.pdf;");
        Response.BinaryWrite(Buffer);
        Response.Flush();
        Response.End();

Open in new window

0
 
LVL 1

Expert Comment

by:narendragummadi
ID: 33530386
Hi phil301,

Does the above solution working for your requirement? If other way, please share it. Thanks.
0
 

Author Comment

by:phil301
ID: 33530534
I have just got into the office this morning, I am about to try it. I am going to have to convert it to VB first though
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:phil301
ID: 33530567
Can you help me with this line?

MyFileStream = new FileStream(fuplXML.PostedFile.FileName.ToString, FileMode.Open)

It doesnt like the fupxml.postefile.filename

is this somehow supposed to be the sql statement that defines the file I want to retrieve fromt the database?
0
 

Author Comment

by:phil301
ID: 33530586
There is no upload control on this portion. It is simply a dropdown box that reads the filename from the database, and a button to retrieve the blob.
0
 
LVL 1

Expert Comment

by:narendragummadi
ID: 33533525
Hi phil301,

In my example i used upload control. For your requirement replace that portion with filestream you are getting from the DB. I assume that you are getting filestream in dropdown selection event. Let me know if my understanding have any gap.
0
 

Author Comment

by:phil301
ID: 33534431
as you can see in my code above, I am using filestream.

  Using fs As New FileStream("c:\" & MyNewFileName, FileMode.Create)
                    fs.Write(buffer, 0, buffer.Length)

just not sure how to implement that.
0
 
LVL 1

Expert Comment

by:narendragummadi
ID: 33535482
As per your code
MyNewFileName = cmd2.ExecuteScalar
what does the string variable MyNewFileName contains?
0
 

Author Comment

by:phil301
ID: 33537730
Select FileName from RetroWorkSheet where ModNumber = '" & Me.RadcmbInstructions.SelectedValue & "'"

Just reading the column that contains the "File Name" , just a string.
0
 
LVL 1

Expert Comment

by:narendragummadi
ID: 33557181
Hi phil301,

Does the below line gets the file content into byte array in your logic?
Dim buffer As Byte() = DirectCast(command.ExecuteScalar(), Byte()

0
 
LVL 1

Expert Comment

by:narendragummadi
ID: 33611650
Hi phil301,

Any progress?
0
 

Accepted Solution

by:
phil301 earned 0 total points
ID: 33613667
I have never been able to figure this out. I had to make a space on the server to download the file to. Not the preferred method, but seems to be working at the moment.

Thanks for your help.
0
 

Author Closing Comment

by:phil301
ID: 36135859
Solution unresolved, other method was used.
0
 
LVL 1

Expert Comment

by:narendragummadi
ID: 36129565
Hi Phil,

Can you share the solution so that it can be helpful.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question