Solved

Downloading BLOB pdf to user machine ASP.NET

Posted on 2010-08-25
14
713 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
Comment Utility
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
Comment Utility
Hi phil301,

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

Author Comment

by:phil301
Comment Utility
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
 

Author Comment

by:phil301
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Expert Comment

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

Author Comment

by:phil301
Comment Utility
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
Comment Utility
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
Comment Utility
Hi phil301,

Any progress?
0
 

Accepted Solution

by:
phil301 earned 0 total points
Comment Utility
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
Comment Utility
Solution unresolved, other method was used.
0
 
LVL 1

Expert Comment

by:narendragummadi
Comment Utility
Hi Phil,

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now