Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Downloading BLOB pdf to user machine ASP.NET

Posted on 2010-08-25
14
Medium Priority
?
722 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

661 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