zoukankan      html  css  js  c++  java
  • WEB API 用MemoryStream流做下载功能

    刚开始把MemoryStream 放在

    var streamResult = new MemoryStream();
    HttpResponseMessage response = new HttpResponseMessage();
    response.Content = new StreamContent(streamResult);
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentDisposition.FileName = filename;
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

      return response;

    发现下载的文件不能正常打开,后在网上查了原因是因为没有把Stram流没有真正转换成功。

    但又不想用FilStream 流,因为这个还要把MemoryStream保存为文件才行,后面找到有一个ByteArrayContent的类,然后把文件转换为byte数组就行了。

    try
    {

    var streamResult = new MemoryStream();
    HttpResponseMessage response = new HttpResponseMessage();
    response.Content = new ByteArrayContent(streamResult.ToArray());
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentDisposition.FileName = filename;
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

    return response;
    }
    catch
    {
    return new HttpResponseMessage(HttpStatusCode.NoContent);
    }

  • 相关阅读:
    QT4.8.7和VS2010环境搭建及使用
    SQL Server--获取磁盘空间使用情况
    SQL SERVER--DBA 常用到的一些脚本
    MySQL--REPLACE INTO与自增
    MySQL--更新自增列的潜在风险
    MySQL--Skip GTID CAP
    MySQL--MHA与GTID
    MySQL--自增列学习
    MySQL--MHA原理
    MySQL--BNL/ICP/MRR/BKA
  • 原文地址:https://www.cnblogs.com/zhian/p/6232047.html
Copyright © 2011-2022 走看看