在 Windows Azure 中轉(zhuǎn)換文檔
Aspose.Words是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺應(yīng)用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
Windows Azure 上的 Aspose.Words 提供了加載、轉(zhuǎn)換和保存文檔的功能。為此,您可以創(chuàng)建一個應(yīng)用程序,該應(yīng)用程序:
- 實現(xiàn)一個簡單的 ASP.NET 表單,使用戶能夠加載文檔并指定所需的輸出格式。
- 調(diào)用 Aspose.Words 來轉(zhuǎn)換文檔并將其發(fā)送回瀏覽器。
本文中描述的應(yīng)用程序作為WebRole實現(xiàn),可以在開發(fā)結(jié)構(gòu)中運行(在開發(fā)人員的機器上)或部署到Windows Azure。此應(yīng)用程序是Aspose.Words如何在云中工作的可能示例之一。
先決條件
- Active Microsoft Azure 訂閱。如果您沒有免費帳戶,請在開始之前創(chuàng)建一個免費帳戶。
- 安裝了 Azure 開發(fā)的 Visual Studio 2019 或 Visual Studio 2017。
轉(zhuǎn)換文檔應(yīng)用程序
本節(jié)討論一個不使用高級Aspose.Words功能或Windows Azure平臺的復(fù)雜服務(wù)的基本項目。
該項目演示了如何使用Aspose.Words輕松構(gòu)建在云中運行的應(yīng)用程序。
創(chuàng)建 Web 角色項目
要創(chuàng)建應(yīng)用程序,您需要執(zhí)行以下步驟:
- 在 Visual Studio 中創(chuàng)建新的云服務(wù)項目。
- 選擇云服務(wù)以具有一個 WebRole 項目。
- 將 NuGet 引用添加到 Aspose.Words。
- 將“文件上載”控件添加到“默認.aspx”窗體,使用戶能夠選擇要上載的文件。
- 將下拉列表控件添加到 Default.aspx 窗體,使用戶能夠選擇輸出格式。
- 為其添加“提交”按鈕和 Click 事件處理程序。
- 修改 ServiceDefinition.csdef 配置文件,以便應(yīng)用程序可以在完全信任下在 Windows Azure 中運行。建議您啟用 NativeCodeExecution = “true”,以避免在使用 Aspose.Words 將文檔轉(zhuǎn)換為 PDF 或 XPS 時可能出現(xiàn)的任何權(quán)限問題。
使用 Aspose.Words 轉(zhuǎn)換文檔的實際代碼僅包含兩行,這些行創(chuàng)建新的 Document 對象以加載文檔,然后使用所需格式調(diào)用 Save 方法。下面的代碼示例演示如何在 Windows Azure 中轉(zhuǎn)換文檔:
using Aspose.Words;
using Aspose.Words.Saving;
using System.Web;
using System;
using System.IO;
namespace WebRole
{
/// <summary>
/// This demo shows how to use Aspose.Words for .NET inside a WebRole in a simple
/// Windows Azure application. There is just one ASP.NET page that provides a user
/// interface to convert a document from one format to another.
/// </summary>
public partial class _Default : System.Web.UI.Page
{
protected void SubmitButton_Click(object sender, EventArgs e)
{
HttpPostedFile postedFile = SrcFileUpload.PostedFile;
if (postedFile.ContentLength == 0)
throw new Exception("There was no document uploaded.");
if (postedFile.ContentLength > 512 * 1024)
throw new Exception("The uploaded document is too big. This demo limits the file size to 512Kb.");
// Create a suitable file name for the converted document.
string dstExtension = DstFormatDropDownList.SelectedValue;
string dstFileName = Path.GetFileName(postedFile.FileName) + "_Converted." + dstExtension;
SaveFormat dstFormat = FileFormatUtil.ExtensionToSaveFormat(dstExtension);
// Convert the document and send to the browser.
Document doc = new Document(postedFile.InputStream);
doc.Save(Response, dstFileName, ContentDisposition.Inline, SaveOptions.CreateSaveOptions(dstFormat));
// Required. Otherwise DOCX cannot be opened on the client (probably not all data sent
// or some extra data sent in the response).
Response.End();
}
static _Default()
{
// Uncomment this code and embed your license file as a resource in this project and this code
// will find and activate the license. Aspose.Wods licensing needs to execute only once
// before any Document instance is created and a static ctor is a good place.
//
// Aspose.Words.License l = new Aspose.Words.License();
// l.SetLicense("Aspose.Words.lic");
}
}
}
測試和運行 Web 角色項目
若要測試和運行示例項目,請執(zhí)行以下步驟:
- 首先,檢查作為簡單 ASP.NET 應(yīng)用程序運行的示例項目。使 WebRole 項目成為解決方案中的啟動項目并運行。Visual Studio將打開一個瀏覽器窗口并將表單加載到其中。您可以上傳示例文檔并驗證轉(zhuǎn)換是否正常工作。
- 然后,您應(yīng)該測試項目是否在計算機上的開發(fā)結(jié)構(gòu)中運行良好。Development Fabric 是 Windows Azure 計算和存儲服務(wù)的本地模擬。選擇“云服務(wù)”項目,使其成為解決方案的“啟動”項目并運行。這一次,構(gòu)建應(yīng)用程序可能需要更長的時間,因為Visual Studio啟動了Development Fabric并將項目部署到其中。瀏覽器窗口將再次打開表單,您將能夠測試應(yīng)用程序。
- 最后,您可以在 Windows Azure 中部署和測試您的項目。為此,您需要有一個有效的 Microsoft Azure 訂閱。 在 Visual Studio 中,右鍵單擊云服務(wù)項目,然后選擇“發(fā)布”。如有必要,請使用您的 Microsoft Azure 帳戶登錄并選擇訂閱。 要進行測試,請選擇暫存環(huán)境,然后單擊發(fā)布。之后,在 Azure 活動日志中,你將收到已部署應(yīng)用程序的 URL。
	 
 
下圖顯示了在 Microsoft Azure 云中運行的 Web 角色項目:
	 
 

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號
渝公網(wǎng)安備
            50010702500608號
             
            
 客服熱線
客服熱線