获取所有产品图片

问题描述 投票:0回答:1

我有一个从数据库中获取产品信息的函数(函数GetProducts)和一个ImageInfo函数,该函数根据分配给它的产品ID获取产品图像。

每个图像的名称是ID @ 0,ID @ 1...。图像的ID与产品ID相同。

[要获取图像,我获取产品ID,并通过它运行查询,以获取名称为ID @ 0的图像。

在这种情况下,我只得到名称为ID @ 0的图像,因为它出现在查询中。

由于每个产品都可以有多个图像,是否可以更改此查询以获取其他图像?其余图像的名称将为ID @ 1,ID @ 2 ....

基本上,我想获取文件夹中的所有图像,而不仅仅是ID @ 0。

我该怎么做?

查询

 @"SELECT CAT.[Path] + '\' + CAST(PRD.ID AS VARCHAR) + '\' + CAST(PRD.ID AS VARCHAR) + '@0.' + 'jpg' AS [Caminho] 
                        FROM [dbo].[Products] AS [PRD] 
                        INNER JOIN[dbo].[Categories] AS[CAT] on PRD.IDCategory = CAT.ID WHERE PRD.Id = @ID";

Component.ts

 GetProducts() {
    let self = this;
    this.Global.refreshToken()
      .subscribe(function (result) {
        self.fileService.getProducts(self.paramProductId)
          .then(function (resultado) {
            if (resultado) {
              self.products = resultado; 
            }
          })
          .then(() => {
            if (self.products) {
              return Promise.all(self.products.map((product) => self.ImageInfo(product.id)));
            }
          })
          .catch((err) => console.error(err));
      });
  }

  ImageInfo(id) {
    var self = this;
    this.Global.refreshToken().subscribe(function (result) {
      self.fileService.getImages(id).then(function (resultado) {
        if (resultado) {
          self.imagMap.set(id,resultado);
        }
      }).catch();

    });
  }

service.ts

getProducts(id): Promise<any> {
        let self = this;
        let urlAux = self.url + "/Products/GetProducts?id=" + id;
        return axios.get(urlAux, {
          headers: {
            Authorization: 'Bearer ' + localStorage.getItem("access_token")
          }
        })
          .then(this.extraData)
          .catch(this.handleErroPromisse);
      }

      getImages(id): Promise<any> {
        let self = this;
        let urlAux = self.url + "/Products/GetImagesFile/" + id;

        return axios.get(urlAux, {'responseType': 'arraybuffer'})
          .then((response) => {
            let image = btoa(
              new Uint8Array(response.data)
                .reduce((data, byte) => data + String.fromCharCode(byte), '')
            );
            return `data:${response.headers['content-type'].toLowerCase()};base64,${image}`;
          });
      }

Service C#

public MemoryStream GetImagesFile(SqlConnection conn, int id, out string contentType, Dictionary<string, string> confRede)
        {
            contentType = "";
            try
            {

                SqlCommand cmd = conn.CreateCommand();
                conn.Open();
                cmd.CommandText = @"SELECT CAT.[Path] + '\' + CAST(PRD.ID AS VARCHAR) + '\' + CAST(PRD.ID AS VARCHAR) + '@0.' + 'jpg' AS [Caminho] 
                    FROM [dbo].[Products] AS [PRD] 
                    INNER JOIN[dbo].[Categories] AS[CAT] on PRD.IDCategory = CAT.ID WHERE PRD.Id = @ID";

                cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;

                string caminho = cmd.ExecuteScalar().ToString();

                NetworkConnection acesso = new Global.Global().AccessToServer(confRede);
                using (acesso)
                {
                    new FileExtensionContentTypeProvider().TryGetContentType(caminho, out contentType);

                    var memory = new MemoryStream();
                    using (var stream = new FileStream(caminho, FileMode.Open))
                    {

                        stream.CopyTo(memory);
                    }
                    memory.Position = 0;
                    return memory;
                }
            }
            catch (Exception ex)
            {

                return null;
            }
            finally
            {
                conn.Close();
            }
        }
c# angular typescript asp.net-core
1个回答
0
投票

您的查询结果类似于path\ID\[email protected],因此似乎每个产品都有用于其图片的独立文件夹,如果是,则可以首先从数据库中查询该文件夹(path\ID),然后循环浏览该文件夹中的所有jpg图像,执行sql查询时不需要合并图像名称。一言以蔽之,您可以通过循环文件的数量来查询数据库的路径后组合文件名。

© www.soinside.com 2019 - 2024. All rights reserved.