Open Office File From Stream

> Edraw Tip > Open Office File From Stream
author
Posted by James Freeman |
Edraw Office Viewer Component allows developers to open MS Word, Excel and PowerPoint document from stream.

The server has not the temporary file if you read file from database.

Download Office Viewer Component and View Sample Projects

Open Office File From Stream C# Sample Project

Open Office File Stream with the HTTP/HTTPS

boolean HttpOpenFileFromStream(BSTR WebUrl, BSTR ProgID, [in, optional] VARIANT WebUsername, [in, optional] VARIANT WebPassword);

WebUrl: A string containing the web url from which downloads data via HTTP. The WebUrl must include the file extend name so that the component knows the file type. For example: https://www.edrawsoft.com/Getfile.aspx?ID=1002&FileName=guid.xls, or https://www.edrawsoft.com/sample.xlsx.
ProgID: Word.Application, Excel.Application or PowerPoint.Application.
WebUsername: A string containing the user name.
WebPassword: A string containing the access password.
Example:

Either you want to open an appointed file or open a file from database, for client side, all you need to do is the same, like the following:

<script language="vbscript">
Sub DownloadFile()
EDOffice.HttpInit();
EDOffice.HttpAddpostString("DocumentID", "Tester.xls");
EDOffice.HttpOpenFileFromStream("http://localhost:2440/ASPCSharp/UploadAction.aspx", "Excel.Application");
End Sub
</script>

Before you call function HttpOpenFileFromStream, you should do two things. One is to initialize http for clearing all parameters and cookies in http, another is to appoint the file or database record. And then use HttpOpenFileFromStream to send the request to the destinated webpage. Before HttpOpenFileFromStream send request, it will add a couple of parameters automatically.
EDOffice.AddPostArgument(L"EDA_GETSTREAMDATA", L"EDA_YES"); This couple of parameters tell the destinated webpage EDOffice will received file as stream.

At the web side, webpage will decide to read which file or database record according to the post parameters. And you should add boundary flag 'EDA_STREAMBOUNDARY' to file data, following is the asp.net demo.

public partial class UploadAction : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params["EDA_GETSTREAMDATA"] == "EDA_YES")
{
//string fullFileName = Server.MapPath("testnewone.txt");
//String fs = File.ReadAllText(fullFileName);
String fullFileName = Server.MapPath(Request.Params["DocumentID"]);
Byte[] fs = File.ReadAllBytes(fullFileName);
Response.Write("Get Stream Successfully!");
Response.Write("EDA_STREAMBOUNDARY");
Response.BinaryWrite(fs);
Response.Write("EDA_STREAMBOUNDARY");
}
else
{
if (Request.Params["author"] == "anyname" && Request.Params["Data"] == "2007-5-15")
{
Response.Write("0\n");
Response.Write("We have receipted the right param from Office ActiveX Control.");
}
if (Request.Files.Count == 0)
{
Response.Write("0\n");
Response.Write("There isn't file to upload.");
Response.End();
}
if (Request.Files[0].ContentLength == 0)
{
Response.Write("0\n");
Response.Write("Failed to receipt the data.\n\n");
Response.End();
}
for (int i = 0; i < Request.Files.Count; i++)
{
string fullFileName = Server.MapPath(Request.Files[i].FileName);
Request.Files[i].SaveAs(fullFileName);
}
Response.Write("Upload Successfully.");
Response.End();
}
}
}

asp:

If l_bWrSreamBoundary
Then Response.Write "EDA_STREAMBOUNDARY"
While Not l_stream.EOS And Response.IsClientConnected
Response.BinaryWrite(l_stream.Read(l_nChunkSize))
Wend l_stream.Close
If l_bWrSreamBoundary
Then Response.Write EDA_STREAMBOUNDARY"

Java:

<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%
int BUFFER_SIZE = 8*1024; // buffer size
java.io.File input_file = null; // input file
java.io.InputStream input_stream = null; // input FileStream
String file_ext = "docx";
int idnum = 0;
idnum = com.dms.RequestParam.getParam(request, "idnum", 0);
String filename = "/webroot/_work/open/doc1.docx";
if(idnum == 222) filename = "/webroot/_work/open/doc2.docx";
ServletOutputStream http_os = null;
try
{
// get the file
input_file = new java.io.File(filename);
// get the input stream
input_stream = new java.io.FileInputStream(input_file);
// setup the response
http_os = response.getOutputStream();
byte[] bytes = new byte[BUFFER_SIZE];
int read = 0;
http_os.print("EDA_STREAMBOUNDARY");
http_os.flush();
// send binary data
while((read = input_stream.read(bytes, 0, BUFFER_SIZE)) != -1)
http_os.write(bytes, 0, read);
http_os.flush();
http_os.print("EDA_STREAMBOUNDARY");
}
catch(Exception ex)
{
;
}
%>

Save MS Word to File Stream

The office viewer component can save the word document without temporary file. Firstly, you can call the WordCopyToClipboard method to save the whole document to clipboard. Then you can store into database as Image as well as in String format.

OA1.WordCopyToClipboard()

IDataObject data = Clipboard.GetDataObject();

text = data.GetData(DataFormats.Text).ToString();

//Or try this using filestream

//FileStream fstream = newFileStream("Sample.doc",FileMode.Open,FileAccess.Read);

//StreamReader sreader = new StreamReader(fstream);

//txtFileContent.Text = sreader.ReadToEnd();

Large File Http Post Upload - PHP and ASP.net

Disables Office 2003 Toolbars

Disable Office Ribbon Button

Show/Hide Office Menu Bar

Integrate MS Office in Delphi Program

Embed Office in C# and Do Office Automation

Online Office Document Viewer

download EdrawMind
main page

Get Started! You Will Love This Easy-To-Use Diagram Software

EdrawMax is an advanced all-in-one diagramming tool for creating professional flowcharts, org charts, mind maps, network diagrams, UML diagrams, floor plans, electrical diagrams, science illustrations, and more. Just try it, you will love it!