Upload a file to a Web server in ASP.NET

EDraw WebOffice Component provides some methods to upload the file to a web server.

HTTP Post

[id(36), helpstring(“Initializes the HTTP connection.”)]
   boolean HttpInit();

[id(37), helpstring(“Adds the post parameter.”)]
   boolean HttpAddpostString([in] BSTR Name, [in] BSTR Value);

[id(38), helpstring(“Adds the post file.”)]
   boolean HttpAddPostFile([in] BSTR LocalFilePath, [in] BSTR NewFileName);

[id(39), helpstring(“Executes the post action.”)]
   boolean HttpPost([in] BSTR WebUrl,[in, optional] VARIANT WebUsername, [in, optional] VARIANT WebPassword);

You can use the follow steps to save the opened office document to a web server.

Sub SavetoServer()
    ‘ASP.NET’
    OA1.HttpInit
    OA1.HttpAddpostString “author”, “anyname”
    OA1.HttpAddpostString “Data”, “2007-5-15”
    OA1.HttpAddPostFile “”, “newfilename.doc”
    OA1.HttpPost “http://localhost:1320/Samples/UploadAction.aspx”    
       
    ‘Or you can call the Save method to upload the openned file to the server directly.For examples:
    ‘OA1.Save “http://localhost:1320/Samples/UploadAction.aspx?FileName=newname
End Sub

Note: If the first parameter of “HttpAddPostFile” is blank, the method will upload the opened office file to server.

Then you need to write a ASP.NET Get page to receipt the file.

Review the follow examples:

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.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();
        }
        string fullFileName = Server.MapPath(Request.Files[0].FileName);
        Request.Files[0].SaveAs(fullFileName);
        Response.Write(“Upload Successfully.”);
        Response.End();
    }
}

How to insert an Office document directly into my Visual Basic form

Microsoft Excel, Microsoft PowerPoint and Microsoft Word support both Object Linking and Embedding (OLE) and ActiveX Document containment. If you want to “host” an Office document in Visual Basic, you can use the OLE Container control and OLE. You can also use the WebBrowser control included with Microsoft Internet Explorer to provide basic ActiveX Document containment. But the simplest and effective method is using the Edraw office Viewer Component. It is a script safe office viewer component. Support more customize methods and events.

Determining if an ActiveX control is safe

Since the digital signature of an ActiveX control stays with the file it was attached to, there is a permanent evidence of the designed intent of the control by the developers. However, this evidence does not account for all possible conditions the control may be used in but were never tested for.

ActiveX controls marked as safe are supposed to be safe in all possible conditions. So a control marked as safe for scripting (SFS) or safe for initialization (SFI) must be written to protect itself from any unpredictable results a script author might unintentionally create when scripting the control. While it is relatively easy for a programmer to create a control with adequate guards to avoid misuse, it is impossible to guarantee that the control is always safe when used with scripting created by another author or programmer.

If a control is marked safe for initializing or safe for scripting, the developers are claiming that no matter what values are used to initialize the control, it will not do anything to damage a user’s system or compromise the user’s security when the control is initialized in any way.

The developer of an ActiveX control should take extra care to ensure that a control is in fact safe before it is marked as safe. For example, each ActiveX control, at a minimum, should be evaluated for the following issues:

  • It does not over-index arrays or otherwise manipulate memory incorrectly, thereby causing a memory leak or corrupt memory region.
  • It validates and corrects all input, including initialization, method parameters, and property setting functions (implements acceptable I/O validation and defense methods)
  • It does not misuse any data about, or provided by, the user
  • It was tested in a variety of circumstances

Embed WORD, EXCEL, POWERPOINT and PDF Easily

It is now possible to embed various Microsoft applications (WORD, EXCEL, POWERPOINT) files as well as PDF into a web page or custom form with Edraw Office Viewer Component. You can download the full install package from http://www.ocxt.com. Then review lot’s example projects in the install folder. The component also provides some office automation interfaces.

More Information

Edraw Office Viewer Component for Hosting Office Documents

Embedding ms office in C# Forms

Host Microsoft Office Application in .NET