Spire.Cloud.Sdk 给开发人员提供了 FileApi 接口,支持操作 Word, Excel, PowerPoint, PDF 等文件,包括文件的上传、复制、移动、下载及删除。本文将介绍如何调用 FileApi 接口来实现以上文件的动作操作。
首先:通过 NuGet 安装 Spire.Cloud.Sdk 包及依赖,详细步骤可参考这篇文章。
其次:通过冰蓝云官网(https://cloud.e-iceblue.cn/)注册账号并登陆,在“我的应用”下点击创建“应用程序”,获得 App ID 及 App Key。
最后:编辑如下 C# 后端代码来实现文件上传、复制、移动、下载、删除以及获取签名的 url 等。
- C#
using Spire.Cloud.Storage.Sdk.Api;
using Spire.Cloud.Storage.Sdk.Client;
using Spire.Cloud.Storage.Sdk.Model;
using System.Collections.Generic;
using System.IO;
namespace CloudStorage
{
class FileApiDemo
{
static string appId = "Your App ID";
static string appKey = "Your App Key";
static string baseUrl = "https://api.e-iceblue.cn";
//配置App ID和App Key
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//创建FileApi实例
static FileApi fileApi = new FileApi(configuration);
//上传文件
public static void UploadFile()
{
//定义上传后的文件路径(如无文件夹,直接写文件名即可)
string path = "uploaded/UploadedFile.docx";
//定义本地文件路径
string localFilePath = "C:/Users/Administrator/Desktop/testfile.docx";
System.IO.Stream data = new FileStream(localFilePath, FileMode.Open);
string storageName = null;
//上传文件
var response = fileApi.UploadFile(path, data, storageName);
data.Close();
}
//复制文件
public static void CopyFile()
{
string srcPath = "input/test.docx";
string destPath = "input/test/test2.docx";
string srcStorageName = null;
string destStorageName = null;
fileApi.CopyFile(srcPath, destPath, srcStorageName, destStorageName);
}
//下载文件
public static void DownloadFile()
{
string path = "input/test.pptx";
string storageName = null;
var response = fileApi.DownloadFile(path, storageName);
}
//移动文件
public static void MoveFile()
{
string srcPath = "input/test.pptx";
string destPath = "output/test/test.pptx";
string srcStorageName = null;
string destStorageName = null;
fileApi.MoveFile(srcPath, destPath, srcStorageName, destStorageName);
}
//删除文件
public static void DeleteFile()
{
string path = "input/test.docx"; ;
string storageName = null;
fileApi.DeleteFile(path, storageName);
}
//获取签名的url
public static void GetSignatureUrl()
{
string path = "input/new.pptx";
string storageName = null;
decimal expiration = 6000;
var response = fileApi.GetSignatureUrl(path, expiration, storageName);
}
}
}
下图为文件上传效果,可在“文档管理”下可查看上传的文档: