Open file from SQLserver

Hi,

I have this situation. I have a file repository in an SQLserver and I also have a web site that can access these files. This is the code to do so:

using (SqlConnection spConn = new SqlConnection())
{
spConn.ConnectionString = docConnectionString;
SqlCommand cmd = new SqlCommand(“SELECT documentContent.PathName() AS FilePath FROM Documents WHERE documentID = '” + id + “’”, spConn);
spConn.Open();
pathName = cmd.ExecuteScalar() as string;

//Obtain a Transaction Context
SqlTransaction transaction = spConn.BeginTransaction(“ItemTran”);
cmd.Transaction = transaction;
cmd.CommandText = “SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()”;
txContext = cmd.ExecuteScalar() as byte[];

//Open and read file using SqlFileStream Class
SqlFileStream sqlFileStream = new SqlFileStream(pathName, txContext, FileAccess.Read);
buffer = new byte[sqlFileStream.Length];
sqlFileStream.Read(buffer, 0, buffer.Length);

//Cleanup
sqlFileStream.Close();
cmd.Transaction.Commit();
}

Now I have the file in the byte array buffer and I can open the file using:

Response.Clear();
Response.Buffer = true;

if (strExtenstion.ToLower() == “doc” || strExtenstion.ToLower() == “docx”)
{
Response.ContentType = “application/vnd.ms-word”;
Response.AddHeader(“content-disposition”, “attachment;filename=” + docName);
}
else if (strExtenstion.ToLower() == “xls” || strExtenstion.ToLower() == “xlsx”)
{
Response.ContentType = “application/vnd.ms-excel”;
Response.AddHeader(“content-disposition”, “attachment;filename=” + docName);
}
else if (strExtenstion.ToLower() == “pdf”)
{
Response.ContentType = “application/pdf”;
Response.AddHeader(“content-disposition”, “attachment;filename=” + docName);
}
else if (strExtenstion.ToLower() == “txt”)
{
Response.ContentType = “application/txt”;
Response.AddHeader(“content-disposition”, “attachment;filename=” + docName);
}
else if (strExtenstion.ToLower() == “msg”)
{
Response.ContentType = “application/vnd.ms-outlook”;
Response.AddHeader(“content-disposition”, “attachment;filename=” + docName);
}
else if (strExtenstion.ToLower() == “png” || strExtenstion.ToLower() == “jpg” || strExtenstion.ToLower() == “jpeg” || strExtenstion.ToLower() == “png” || strExtenstion.ToLower() == “gif”)
{
Response.ContentType = “application/paint”;
Response.AddHeader(“content-disposition”, “attachment;filename=” + docName);
}

Response.Charset = “”;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.End();

This will make the client browser promt and ask if the file should be opened or saved to disk. All this works fine but I would like to improve it and my question is:

Can I use Aspose to open for instance a word dokument directly without getting the promt from the browser? I have the file in the byte array (buffer) and the filename (docName). If this is possible - for which file formats can I do this (office, pdf, txt, images, … )?

Best regards
Robert Tell

Best regards

Hi

Thanks for your request. Sure you can open documents directly from database using Aspose components. Please see the following link for more information:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-load-and-save-a-document-to-database.html

But you should note, Aspose.Words is a class library, which allows working with MS Word documents programmatically. Aspose.Words does not provide any user interface for viewing or editing documents.

However it is possible to create Document preview, for example by converting your document to image, XPS or SWF format.

Best regards,