Solved

Downloading BLOB pdf to user machine ASP.NET

Posted on 2010-08-25
14
721 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

631 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