How should I be saving .doc documents to a SQL Server database - c#

I am saving a .doc file to a SQL Server database as varbinary(max) using the C# code below.
I am able to save the file, but when I retrieve the file back and want to display the contents on the web page, my code is downloading the file and I am in great confusion about how to handle it.
The exact functionality I am looking for is the way naukri.com uploads the resume and gives a preview of it. My code is :
byte[] fileContent = new byte[fuResume.PostedFile.ContentLength];
fuResume.PostedFile.InputStream.Read(fileContent, 0, fuResume.PostedFile.ContentLength);
//lblAppliedMessage.Text = ByteArrayToString(fileContent);
//lblAppliedMessage.Text = BitConverter.ToString(fileContent).Replace("-", string.Empty);
byte[] btYourDoc;
btYourDoc = fileContent;
Response.ContentType = "application/ms-word";
Response.AddHeader("Content-Disposition", "inline;filename=yourfilename.doc");
Response.OutputStream.Write(btYourDoc, 0, fileContent.Length);
Response.BinaryWrite(btYourDoc);
Response.End();

The reason your file is getting downloaded instead of displayed is because you're setting the content type to application/ms-word. This tells a browser to download the file (they can't natively handle files of that type so they delegate to an external app).
You'll need to have code that knows how to interpret the MS Word format and convert that to something viewable in a browser (HTML, some kind of plugin that will do that for you, etc). Saving the raw Word document and then sending it back to the client in the same state is basically just having them download a Word file.

squillman is right. There are tons of third party components that do Word -> HTML conversion.
One other option, which may be more appropriate for an intranet site, is to install Word on the server.
An example of this is here:
http://www.c-sharpcorner.com/UploadFile/munnamax/WordToHtml03252007065157AM/WordToHtml.aspx
Effectively, the doc is opened, saved out as HTML, then subsequent requests can retrieve the HTML version of the file for preview.
Office automation server side has many pitfalls, however - see http://support.microsoft.com/kb/257757 for more information.

Here's a good one where the end result it's up to the user whether to download or view the file here's the link but #Squillman is right by putting the Response headers you're telling it to download.

Related

Control display of streamed PDF in browser

Using info found here and elsewhere I have successfully implemented an app that will pull a PDF from our document storage server, stream it across our firewall and display it in a frame on the client's web browser. The PDF is loaded into a page using the following code and that page is the source for the iframe.
int ImageID;
if (int.TryParse(Request.QueryString["ImageID"], out ImageID))
{
FileTransferService.FileTransferClient client = new FileTransferService.FileTransferClient();
Byte[] documentStream = client.GetFile(Classes.AppSettings.pwServer, Classes.AppSettings.pwDatabase, Classes.AppSettings.pwUsername, Classes.AppSettings.pwPassword, Classes.AppSettings.pwCabinet, ImageID, "", "");
Response.ContentType = "application/pdf";
Response.BinaryWrite(documentStream);
}
This is working fine most of the time, but due to settings on the client for the Acrobat Reader plugin the PDF will sometimes open in Acrobat Reader rather than displaying in the web page, leaving our page with an empty hole where the PDF should be. I've searched for a third party PDF viewer we could use in the app without success. Either they don't accept a stream as a source or the image quality is unacceptable. If we could force it to always pop up in Acrobat Reader that would be acceptable, but we'd prefer it to be displayed in the web page. For security reasons we don't want to write the file to disk and display it from there.
Is there a method to force the viewing behavior one way or another or a third party viewer we could use that will solve this problem?
As an alternative to the optimal solution, you could add the 'Content-Disposition' header to make the browser always treat it as a download.
Usage:
Content-Disposition: attachment; filename="example.pdf"
This may also be worth looking into, although I've never used it:
System.Net.Mime.ContentDisposition
Represents a MIME protocol Content-Disposition header.

Generating pdf using generic handler (ashx)

I got a requirement to create pdf on the fly from the data stored in database.
I am using html, jquery and WCF in my application.
I don't find a way to generate pdf (Show in a browser or as an attachment) using client technology (jquery, or any other client plugins). I tried to use pdf.js, but could not able to succeed. Later I used .net generic handler to generate pdf. I was passing bytearray to handler, in turn pdf started rendering on client.
I got some random issue, then I wrote a sample application to make sure handler is working fine. I used same code with a static text to generate pdf, but it started throwing a error on the client.
Below is the code snippet I used
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("content-disposition", "inline; filename=download");
context.Response.Write("Hello World");
//context.Response.BinaryWrite(System.Text.Encoding.Default.GetBytes("Hello world"));
//context.Response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes("Hello world"));
context.Response.Flush();
context.Response.End();
I am getting errors on all the browsers
Failed to load pdf document on Chrome.
An error occurred while loading the PDF. PDF.js v0.8.505 (build: da1c944) Message: InvalidPDFException - In mozilla firefox
File does not begin with '%PDF-' in IE.
Note: I tried with aspx page too. I cant create physical file due security issues. If there is any better way of achieving it please let me know. Thanks in advance.
PDF is a standard file format.You cannot write a normal text file and rename it to filename.pdf and wish it to work.
You will need .Net libraries for that.There are lot try googling .Net pdf library
Some Info.
pdf.js is just for viewing in browser.
setting context.Response.ContentType just tells the browser the file its gonna receive,So it can use existing mapped application to open it.Like Adobe browser plugin for pdf file.

editable word document attachment

Its a general scenario when we provide an option of attaching a file (MS .doc) to end user. This file is stored in DB as binary. When user try to access this attachment next time, we allow them to download it. Now, here I want to give a feature to user where he should be able to open this doc file on click, edit it and save it without downloading.
.doc is a binary format and not easy to work with - a library such as Aspose, as mentioned by Christian, is definitely the way to go.
However, if .DOCX is acceptable (and that's Office 2007 and higher), then you can achieve what you want in three steps:
Convert .docx to HTML
Convert Word to HTML then render HTML on webpage
Display the HTML using any rich text control of your choice
What is the best rich textarea editor for jQuery?
Finally, convert HTML back to .docx:
Convert Html to Docx in c#
You would have to "reinvent" Microsoft Office Online (look into your skydrive account). I am unsure if there are any "out of the box" libraries for that, but you could build a simple editing app by leveraging Aspose word (or some other library). But that would be far from simple.
Link to aspose: http://www.aspose.com/.net/word-component.aspx
Word will only open files that are locally stored. What you are looking for is something similar to editing items that SharePoint provides using the WebDAV interface.
You may be able to use this approach to support your requirement. You should be cautious about the security aspects of the solution unless you have fully authenticated access to the shared folder on the server.
I am not sure if a standalone MS Word Document editor exists. However, this can be done with using a combination of rich text formatting / converting tool (for example, the DevExpress ASPxHtmlEditor + Document Server):
Load binary data from a DB;
Import loaded data (MS Word content) as HTML content into the ASPxHtmlEditor;
Edit imported data via the WYSIWYG ASPxHtmlEditor;
Convert the edited HTML back to MS Word content;
Save the converted / edited MS Word content back to the DB.
I believe, it is possible to do something like this if you have such products (free or commercial analogs) in your project.

Downloading file from database where file is stored in binary format

I have got stuck in a problem while downloading documents from database.
Currently I'm working in ASP.net project and this is my first career project.
We have some documents which we store in database. The documents(.pdf,.doc,.png,.docx,xls,xlsx) are stored in binary format with their type specified.
I can download one document using Response.write. But now i have to concatenate some documents and then allow user to download on button click.
I have googled a lot. Developers have said that this is impossible. But still i feel that this can be done.
However if this is impossible i thought that i will save these individual documents first at some server location and then zip them and then allow user to download. But how would i be able to save the individual document in their original format at server location.
Please help me out. I'm in big problem.
It is not impossible to read more than one document from a database and zip them up before presenting the zip file to the response stream. You can do this all in process, there is no need to save the documents to the server.
This code uses Ionic.Zip to zip up several files and write them to a MemoryStream:
foreach (var file in files)
{
zipFile.AddEntry(file.FileName, file.ContentBytes); // these are the file bytes
}
var zipMs = new MemoryStream();
zipFile.Save(zipMs);
zipMs.Seek(0, SeekOrigin.Begin);
zipMs.Flush();
The file.FileName includes the extensions (.docx) of the documents and when downloading through the browser, everything is displayed and saved correctly.

How to Open MemoryStream File In Browser?

I want to create a Text file in my code behind file in my web application. However, I am not allowed to save this file to server. So I tried to use MemoryStream class to save my file into memory. So far I have,
MemoryStream memoryStream = new MemoryStream();
TextWriter textWriter = new StreamWriter(memoryStream);
textWriter.WriteLine("Something");
memoryStream.Close();
It seems like working but my requirement is to open this file on client browser when he/she clicks button. Since this file does not have a physical path like ..../text.txt. I have no idea how to open it on browser.
How can I do this in ASP.Net using C#. I searched a lot but could not find a solution working for me.
Thanks in advance.
This is a lot easier than you think. Keep in mind that the HTTP protocol doesn't actually transfer "files" in the strictest sense of the word. It transfers requests and responses, each containing headers and content. In this case, you're concerned with the headers and content of the response.
The easiest way to do this in a WebForms application is with a generic handler. Specifically, take a look at the implementation of the handler's response in that link:
context.Response.ContentType = "image/png";
context.Response.WriteFile("~/Flower1.png");
This is writing the content of an image file to the response after setting the response's header appropriately. What you want is closer to what's commented out in the implementation:
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
This would send to the browser plain text, nothing more. The browser won't think it's a web page or anything like that, won't apply any styles to it, etc. As far as the web browser is concerned, it just downloaded a text file with the words "Hello World" in it. You can Response.Write() all the text you want to build that file.
You can further customize your response headers to give the browser even more information. For example, if you add the following header to your HttpResponse:
Content-Disposition: attachment; filename=myfile.txt
Then the browser will translate that to mean that this "file" should be downloaded and saved, not just displayed. (Of course, the user's browser settings may tell it to display it anyway, but this is the proper way for the server to "suggest" to the browser that it should save the file.)
From the point of view of the browser, it doesn't matter where the "file" came from. Whether it was from the server's file system or dynamically generated or magically conjured, it makes no difference. The browser is concerned only with the response headers and content. If the headers say it's text, and they say that it's a file, then the content will be treated as a text file.
Why do you need to write a MemoryStream? Just write it to the HTTP response if you want to send it to the browser.
Response.WriteLine("Something");
If you want to make the browser download this response as a file, see here.
I honestly believe this isn't a good pattern in Web development.
It's just about reading your file and send its data as text to the client-side (Web browser), edit it in a textbox, send back modified text and save it as file in the path or storage of your choice.
HTTP is an stateless protocol, so you won't be leaving a file opened in the server-side while its contents are edited in client-side, as both tiers are absolutely disconnected after server response ends.
Ok, I think I figured out what you want. You say you have a button, with witch you want to go to the content of a text-file, that you want to create in-memory, but you don't know what url to send the browser to when the user clicks the button?
If this is the case here's what you can do:
1) On the page that has the button, set the href (or link-location or whatever) of the button to be a new asp.net page (jet to be created). Something like "textfile.aspx" or whatever. Also, remove all the code regarding the memory-stream.
2) Create the new asp.net file (textfile.aspx, or whatever you decided to call it). The content of that file should be like this:
Response.WriteLine("Something"); // Or whatever you previously wrote to the MemoryStream
The point is, you should separate into two different files (or separate action based on query-string).

Categories