Display .DOC file in Varbinary

G

GaryDean

I'm storing Images and MSWord doc files in Varbinry columns. I have no
problem getting and displaying the images but I'm looking for the right
approach to allowing the user to download the MSWord doc in the varbinary.
I've provided download of .doc files from my own directories but never from
the file system or a varbinary.

Anyone know how to do that?
 
W

Walter Wang [MSFT]

Hi Gary,

I hope I haven't misunderstood your question: I think for a msword document
from the file system or from varbinary column, you just read them into a
binary byte array and use Response.BinaryWrite to write to response stream.
The only thing different from writing an image is the ContentType and
Content-Disposition header:

Response.AddHeader("Content-Disposition", "attachment;filename=blob.doc")
Response.ContentType = "application/msword"


Please refer to following KB for more info:

#HOW TO: Read and Write BLOB Data by Using ADO.NET Through ASP.NET
http://support.microsoft.com/kb/326502


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

GaryDean

Walter:
the example at kb/326502 works great for downloading a .doc file from a
varbinary with a word document in it. However, when I convert the same code
to c# and use it in my project I get an I.E. error that says:

"the data necessary to complete this operation is not yet available" ??

But the "myData" variable contains exactly the same contents in both cases.
I tried Response.ClearContent() and Response.ClearHeaders() but that made no
difference. In my real project the page is more complex than in the
example.

I pasted the two code snippets below but I don't think it matters since the
stream that I am writing is identical down to the byte.

Thanks,
Gary


Below is the vb code from the example that
works..............................................................

Protected Sub Button3_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim con As New SqlConnection("Data Source=(local);Initial
Catalog=PTPEmployment;Integrated Security=True")
Dim da As New SqlDataAdapter("Select * From resumes where
candidateid = 'fred'", con)
Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet()

con.Open()
da.Fill(ds, "resumes")
Dim myRow As DataRow
myRow = ds.Tables("resumes").Rows(0)

Dim MyData() As Byte
MyData = myRow("resumeblob")

Response.Buffer = True
Response.ContentType = "application/msword"
Response.BinaryWrite(MyData)

MyCB = Nothing
ds = Nothing
da = Nothing

con.Close()
con = Nothing
End Sub




Below is my C# Code that gets the browser
error.................................................................................

protected void btnGetResume_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new
SqlConnection((string)HttpContext.Current.Session["PTPConnectionString"]))
{
conn.Open();
string mySQL = "Select * From resumes where candidateid = '" +
(string)Session["candidateid"] + "'";
SqlDataAdapter da = new SqlDataAdapter(mySQL, conn);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet();

da.Fill(ds, "resumes");
DataRow myRow;
myRow = ds.Tables["resumes"].Rows[0];
byte[] MyData = null;
MyData = (byte[])myRow["resumeblob"];

Response.Buffer = true;
Response.ContentType = "application/msword";
Response.BinaryWrite(MyData);

MyCB = null;
ds = null;
da = null;

conn.Close();

}

}

--
"Walter Wang [MSFT]" said:
Hi Gary,

I hope I haven't misunderstood your question: I think for a msword
document
from the file system or from varbinary column, you just read them into a
binary byte array and use Response.BinaryWrite to write to response
stream.
The only thing different from writing an image is the ContentType and
Content-Disposition header:

Response.AddHeader("Content-Disposition", "attachment;filename=blob.doc")
Response.ContentType = "application/msword"


Please refer to following KB for more info:

#HOW TO: Read and Write BLOB Data by Using ADO.NET Through ASP.NET
http://support.microsoft.com/kb/326502


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.
 
G

GaryDean

Walter: This just in...
I practically solved the problem by response.redirecting to another page
where the code worked fine in the load event. so the problem is now only of
academic interest so you might just want to forget about it.
thanks for the help
Gary
 
W

Walter Wang [MSFT]

Thanks Steve for your input.

Hi Gary,

As Steve's article shows, it's important to call Response.Clear() and
Response.End() at the begin and end; otherwise the downloaded file might
not have the correct data.

Let me know if you have further questions.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top