Http Upload Office Files - upload Word, Excel, PowerPoint file to server with HTTP/HTTPS

Office Viewer Component allows developers to upload Word, Excel and PowerPoint file with HTTP/HTTPS.

HTTP definitely puts less burden on your clients. A lot of places have proxies or firewalls that block all FTP traffic (in or out). The big advantage of HTTP is that it goes over firewalls and it's very easy to encrypt.

Download Office Viewer Component and View Sample Projects

Http Methods

boolean HttpInit()

Initializes the HTTP connection.

boolean HttpAddPostString(BSTR Name, BSTR Value)

Adds the post parameter.
Name: The parameter name.
Value: The parameter value.

boolean HttpAddPostFile(BSTR LocalFilePath, [in, optional] VARIANT NewFileName)

Adds the post file.
LocalFilePath: The full file path needs to upload to the server. It the parameter is NULL, the function will add the file which is opening in the component.
NewFileName: The new file name for the upload file.

boolean HttpAddPostOpenedFile([in, optional] VARIANT NewFileName)

Adds the opened office file to post queue.
NewFileName: The new file name for the upload file.

boolean HttpPost(BSTR WebUrl, [in, optional] VARIANT WebUsername, [in, optional] VARIANT WebPassword)

Sends the specified request to the HTTP server.
WebUrl: A string containing the web URL from which uploads data via HTTP.
WebUsername: A string containing the user name.
WebPassword: A string containing the access password.

Http Post Examples

The following code demos how to upload the opened file to server with the HTTP mode. It can also post multiple files in a post request.

<script language="vbscript">
Sub UploadFile()
EDOffice.HttpInit
EDOffice.HttpAddpostString "author", "anyname"
EDOffice.HttpAddpostString "Data", "2010-5-15"
EDOffice.HttpAddPostOpenedFile
EDOffice.HttpPost "http://localhost:1320/Samples/UploadAction.aspx"
End Sub
</script>

Save as docx file then upload.

<script language="vbscript">
Sub UploadFile()
EDOffice.HttpInit
EDOffice.HttpAddpostString "author", "anyname"
EDOffice.HttpAddpostString "Data", "2010-5-15"
EDOffice.HttpAddPostOpenedFile "newname.docx", 12
EDOffice.HttpPost "http://localhost:1320/Samples/UploadAction.aspx"
End Sub
</script>

Save as doc file then upload.

<script language="vbscript">
Sub UploadFile()
EDOffice.HttpInit
EDOffice.HttpAddpostString "author", "anyname"
EDOffice.HttpAddpostString "Data", "2010-5-15"
EDOffice.HttpAddPostOpenedFile "newname.doc", 0
EDOffice.HttpPost "http://localhost:1320/Samples/UploadAction.aspx"
End Sub
</script>

Or you can save your file to server with the HttpAddPostFile method.

OA1.Save()
OA1.CloseDoc()
OA1.HttpInit()
OA1.HttpAddPostFile "d:\1.doc"
document.OA1.HttpPost("http://localhost:1833/test/UploadAction.aspx");

Or:

OA1.SaveAs("d:\1.doc", 0)
OA1.SaveAs("d:\2.doc", 0)
OA1.HttpInit()
OA1.HttpAddPostFile "d:\1.doc"
document.OA1.HttpPost("http://localhost:1833/test/UploadAction.aspx");

Note: If you try to save the opened file to remote server by the "HTTP" methods, you need to write a receipt page in your web server. Because the component uploads the file by HTTP mode. The following is some sample code.

Server Code to Receive the Http Request

ASP.NET Example

//Default.aspx
<script language="vbscript">
Sub SavetoServer()
EDOffice.HttpInit
EDOffice.HttpAddpostString "author", "anyname"
EDOffice.HttpAddpostString "Data", "2010-5-15"
EDOffice.HttpAddPostOpenedFile
EDOffice.HttpPost "http://localhost:1320/Samples/UploadAction.aspx"
'EDOffice.Save "http://localhost:1320/UploadAction.aspx?author=name&Data=2"
End Sub
</script>

//UploadAction.aspx.cs file

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;

public partial class UploadAction : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request. QueryString["author"] == "name" && Request. QueryString["Data"] == "2")
{
Response.Write("0\n");
Response.Write("We have receipted the right param from Edraw Diagram 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();
}
string fullFileName = Server.MapPath(Request.Files[0].FileName);
Request.Files[0].SaveAs(fullFileName);
Response.Write("Upload Successfully.");
Response.End();
}
}

PHP Example

<?php
header("http/1.1 200 OK");
$user = iconv("UTF-8", "UNICODE", $_POST['user']);
$passwd = iconv("UTF-8", "UNICODE", $_POST['passwd']);
$sql = sprintf("username=%s passwd=%s", $user,$passwd);
echo $sql;
$sql = sprintf("file=%s size=%s error=%s tmp=%s", $_FILES['trackdata']['name'],$_FILES['trackdata']['size'],$_FILES['trackdata']['error'],$_FILES['trackdata']['tmp_name']);
echo $sql;
$handle = fopen($_FILES['trackdata']['name'],"w+");
if($handle == FALSE)
{
exit("Create file error!");
}
$handle2 = fopen($_FILES['trackdata']['tmp_name'],"r");
$data = fread($handle2,$_FILES['trackdata']['size']);
echo $data;
fwrite($handle,$data);
fclose($handle2);
fclose($handle);
exit(0);
?>

ASP: (review the full code in the install folder\samples\asp\)

<%@Language=VBScript %>
<!-- #include file="./include/upload.inc" -->
<!--#include file="./include/conn.asp"-->
<%
Set Uploader = New UpFile_Class
Uploader.NoAllowExt="cs;vb;js;exe"
Uploader.GetData (Request.TotalBytes)
Request.TotalBytes
if Uploader.isErr then
select case Uploader.isErr
case 1
Response.Write "Fail to receipt the data."
case 2
Response.Write "The file is too big to upload"
End select
'Response.End
End if
Dim id
If "" <> Request.QueryString("id") then
id = Request.QueryString("id")
End if
if id<>0 then
Sql="SELECT * from doc where doc.id = "&id
else
Sql="SELECT * from doc"
End if
rs.Open Sql,conn,1, 3
for each formName in Uploader.file
set file=Uploader.file(formName)
If id<>0 then
If("" = Request.QueryString("isAip")) Then
rs("DocContent") = Uploader.FileData(formname
rs("DocID") = Uploader.Form("DocID")
rs("DocTitle") = Uploader.Form("DocTitle")
rs("DocType") = Uploader.Form("DocType")
Else rs("AipContent") = Uploader.FileData(formname)
rs("state") = 2
End if
Else
rs.AddNew
rs("DocID") = Uploader.Form("DocID")
rs("DocTitle") = Uploader.Form("DocTitle")
rs("DocContent") = Uploader.FileData(formname)
rs("Docdate") = Now()
rs("DocType") = Uploader.Form("DocType")
rs("state") = 1
End If
set file=nothing
rs.Update
exit for
next
rs.Close
Set rs=Nothing
conn.close
set conn = Nothing
Set Uploader = Nothing
%>

JSP Example

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page language="java" import="com.jspsmart.upload.File>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
String sPath="C:\\"
mySmartUpload.initialize(pageContext);
mySmartUpload.upload();
String TempName=mySmartUpload.getRequest().getParameter("TempName");
mySmartUpload.save(sPath);
File myFile =mySmartUpload.getFiles().getFile(0);
%>

Http Open File From Stream

FTP Upload Download Office Files

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

Embed MS Office in WPF Program

Office ActiveX Control

Get Started! Make your application display and interact with MS Office. Try Office Viewer Component
Top