js中将内容类型设置为图像

js中将内容类型设置为图像

本文介绍了在JavaScript v10 SDK Node.js中将内容类型设置为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用Azure JS SDK v10上传图像时,它将图像显示为应用程序/八位字节流.

这是我根据天青官方文档最初所做的

 异步函数uploadLocalFile(堕胎者containerClient,文件路径,file_guid,)filePath = path.resolve(filePath);//图像文件路径const fileName = file_guid;//包含文件名const blobClient = containerClient.getBlobClient(fileName);const blockBlobClient = blobClient.getBlockBlobClient();返回await blockBlobClient.uploadFile(filePath,aborter);} 

这是上传图像的应用程序/八位字节流

然后我尝试了此操作,设置了标头并尝试将其设置为image/jpeg,但这仍然将内容类型设置为application/octetstream.

 filePath = path.resolve(filePath);const fileName = file_guid;const blobClient = containerClient.getBlobClient(fileName);const blockBlobClient = blobClient.getBlockBlobClient();const blobOptions = {blobHTTPHeaders:{blobContentType:'image/jpeg'}};返回await blockBlobClient.uploadFile(filePath,aborter,blobOptions);} 

在上传到天蓝色的Blob存储中时,有什么方法可以将图像的内容类型设置为image/jpeg吗?

解决方案

我使用您的代码进行测试并设置了 httpHeaderOptions ,您可以参考以下界面说明:

When I try to upload images using the Azure JS SDK v10, it shows them as application/octetstream.

This is what I have done initially according to the official azure documentation

 async function uploadLocalFile(
    aborter,
    containerClient,
    filePath,
    file_guid,
  )

    filePath = path.resolve(filePath);  //Image file path
    const fileName = file_guid; //contains file name
    const blobClient = containerClient.getBlobClient(fileName);
    const blockBlobClient = blobClient.getBlockBlobClient();


    return await blockBlobClient.uploadFile(filePath, aborter);

  }

This was uploading the images s application/octetstream

Then I tried this, setting the headers and attempting to make it as image/jpeg, but still this makes the content type as application/octetstream.


    filePath = path.resolve(filePath);
    const fileName = file_guid;
    const blobClient = containerClient.getBlobClient(fileName);
    const blockBlobClient = blobClient.getBlockBlobClient();

    const blobOptions = { blobHTTPHeaders: { blobContentType: 'image/jpeg' } };

    return await blockBlobClient.uploadFile(filePath, aborter, blobOptions);

  }

Is there any way to make the images' content type as image/jpeg while uploading to azure blob storage?

解决方案

I test with your code and set httpHeaderOptions, you could refer to this interface description:BlobHTTPHeaders, below is my test code.

const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
const path = require("path");
// Enter your storage account name and shared key
const account = "xxxx";
const accountKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";

const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  sharedKeyCredential
);

const containerName = "container";

async function main() {
     const containerClient = blobServiceClient.getContainerClient(containerName);

    const filePath = path.resolve("C:\\xxxxx\\me.jpg");
    const fileName = "bbb";
    const blobClient = containerClient.getBlobClient(fileName);
    const blockBlobClient = blobClient.getBlockBlobClient();
    const blobOptions = { blobHTTPHeaders: { blobContentType: 'image/jpeg' } };
    const uploadBlobResponse =await blockBlobClient.uploadFile(filePath,blobOptions);

    console.log(`Upload block blob test.txt successfully`);
}

main();

这篇关于在JavaScript v10 SDK Node.js中将内容类型设置为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:34