【问题标题】:How to upload files from a react application to azure file share under storage account?如何将文件从 React 应用程序上传到存储帐户下的 azure 文件共享?
【发布时间】:2022-01-22 23:40:27
【问题描述】:

我正在尝试将文件从本地计算机上传到我通过反应应用程序创建的存储帐户下的 azure 文件共享。我可以按照提供的步骤在此处创建文件和上传: https://docs.microsoft.com/en-us/javascript/api/overview/azure/storage-file-share-readme?view=azure-node-latest

但无法从我的本地机器上传文件。

【问题讨论】:

  • 请编辑您的问题并包括您编写的代码和您遇到的问题。

标签: javascript reactjs angular azure azure-files


【解决方案1】:

创建 Azure 文件后,您可以使用以下代码将任何本地文件上传到 azure 文件共享。

在 main() 中输入您的存储帐户名称、SAS 和指向本地文件的路径。

// Create a share
const shareName = `newshare${new Date().getTime()}`;
const shareClient = serviceClient.getShareClient(shareName);
await shareClient.create();
console.log(`Create share ${shareName} successfully`);

// Create a directory
const directoryName = `newdirectory${new Date().getTime()}`;
const directoryClient = shareClient.getDirectoryClient(directoryName);
await directoryClient.create();
console.log(`Create directory ${directoryName} successfully`);

// Upload local file to Azure file parallelly
const fileName = "newfile" + new Date().getTime();
const fileClient = directoryClient.getFileClient(fileName);
const fileSize = fs.statSync(localFilePath).size;

// Parallel uploading with ShareFileClient.uploadFile() in Node.js runtime
// ShareFileClient.uploadFile() is only available in Node.js
await fileClient.uploadFile(localFilePath, {
rangeSize: 4 * 1024 * 1024, // 4MB range size
parallelism: 20, // 20 concurrency
onProgress: (ev) => console.log(ev)
});
console.log("uploadFile success");

【讨论】:

  • 谢谢@Rajkumar。我还想在uploadData() 方法中使用onProgress 来跟踪上传进度。但我只取得了一次进展,即上传完成后。你能建议其他方法吗?
猜你喜欢
  • 2022-11-11
  • 2016-06-16
  • 2021-02-22
  • 1970-01-01
  • 2017-04-11
  • 2020-01-20
  • 2018-08-09
  • 2020-08-21
  • 2020-02-13
相关资源
最近更新 更多