• c# Image/Pdf 预览


    1. 将图片的路径转成流形式

    public async Task<ByteArrayContent> GetPdf(long id)
            {
                try
                {
                    var image = await AgentProxy.Instance.StockMaterialImageAgent.GetById(id);
                    var filePath = image.FilePath;
                    //ensure that the path is a correct path;
                    if (!System.IO.File.Exists(filePath))
                        return new ByteArrayContent(new byte[] { });
    
                    using (var fs = new FileStream(filePath, FileMode.Open))
                    {
                        var content = new byte[fs.Length];
                        await fs.ReadAsync(content, 0, content.Length);
                        return new ByteArrayContent(content);
                    }
                }
                catch (Exception exception)
                {
                    Logger.WriteErrorLog(exception);
                    return new ByteArrayContent(new byte[] { });
                }
            }
    

    2.  补充头部信息

     [HttpGet]
            [AllowAnonymous]
            [Route("getPdf/{id}")]
            public async Task<HttpResponseMessage> GetPdf(long id)
            {
                var content = await stockTakeBusiness.GetPdf(id);
                var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = content };
                result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                return result;
            }
    

    3 请求

    const getPdf = function (id) {
            const url = `${baseUrl}api/stockTake/getPdf/${id}`;
            return $http({
                url: url,
                responseType: "arraybuffer",
                method: "GET"
            });
        };

    4 查看图片详情

    let filePath = attachment.FilePath;
            $all.$stockTake.getPdf(attachment.Id)
                .then(function (response) {
                    let type = "";
                    let suffix = getFilePathSuffix(filePath);
                    if (suffix.toLowerCase() == "pdf") {
                        type = 'application/pdf';
                    } else {
                        type = `image/${suffix}`;
                    }
                    if (response.status === 200) {
                        var file = new Blob([response.data], {
                            type: type
                        });
                        var fileURL = URL.createObjectURL(file);
                        window.open(fileURL);
                    }
                });   
    

      

  • 相关阅读:
    ElementUI 之 Message,自动弹出,信息不显示问题
    eslint 对下一行不要校验报错
    <input type="file"> accept属性筛选文件类型
    纯 css 控制隔行变色
    本地启动服务,两个进程分别监听两个端口,导致两个 URL 不同
    tap 事件会触发两次问题
    时间宝贵-----
    有些人,得到和失去,你都会后悔!
    前调清新,中调醇厚,后调悠长。
    office 格式定义
  • 原文地址:https://www.cnblogs.com/zxhome/p/11314582.html
Copyright © 2020-2023  润新知