簡體   English   中英

獲取文件夾中的所有圖像

[英]Get all images in folder

我有一個從getProduct數據庫獲取產品信息的函數,以及一個ImageInfo函數,該函數根據分配給它的產品ID獲取產品圖像。

每個圖像的名稱為ID @ 0,ID @ 1...。該ID與產品ID相同。

為了獲得圖像,我獲得了產品ID,並通過它運行查詢,在該查詢中獲得名稱為ID @ 0的圖像。

如何從文件夾中獲取所有圖像? 就是說,現在我不只是想要ID @ 0,現在我想獲取所有請求,並在可能的情況下使用不同的請求(出於性能原因,在同一請求中不接收所有請求)。

誰能幫我?

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();

    });
  }

服務

 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}`;
          });
      }

服務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#中使用System.IOGetFiles(string path)方法來檢索目錄的內容。

您將得到一個字符串數組,其中包含目錄中每個文件的文件名。

參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM