0

我们正在控制器类中以字节的形式下载 word 文档,我们希望将字节数组传递给视图,然后使用 Javascript FileSystemObject 和 Active X Object (Word) 打开 word 文档。

我不确定如何将字节数组传递给视图。

4

2 回答 2

0

您需要创建一个操作方法,该方法返回return File(byte[]);并编写 JavaScript 以定位该新 URL。

要将字节数组传递给视图,只需return View(byte[])在控制器操作方法中使用。

于 2013-05-23T18:23:35.360 回答
0

使用FileStreamResult

public FileStreamResult MyView() {

    byte[] bytearr;
    MemoryStream m = new MemoryStream(bytearr);

    return File(m, "mime", "filename");   
}

或者你可以写一个ActionResult这样的自定义:

public ActionResult MyView() {
    byte[] bytearr;
    MemoryStream m = new MemoryStream(bytearr)
    return new MemoryStreamResult(m, "mime", bytearr.Length, "filename");
}


public class MemoryStreamResult : ActionResult
{
    private MemoryStream _stream;
    private string _fileName;
    private string _contentType;
    private string _contentLength;



    public MemoryStreamResult(MemoryStream stream, string contentType, string contentLength, string fileName = "none")
    {
         this._stream = stream;
         this._fileName = fileName;
         this._contentType = contentType;
         this._contentLength = contentLength;
    }

    public override void ExecuteResult(ControllerContext context)
    {
         context.HttpContext.Response.Buffer = false;
         context.HttpContext.Response.Clear();
         context.HttpContext.Response.ClearContent();
         context.HttpContext.Response.ClearHeaders();

         context.HttpContext.Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", this._fileName));

         if (!String.IsNullOrEmpty(this._contentLength))
            context.HttpContext.Response.AddHeader("content-length", this._contentLength);

         context.HttpContext.Response.ContentType = this._contentType;

         this._stream.WriteTo(context.HttpContext.Response.OutputStream);
         this._stream.Close();

         context.HttpContext.Response.End();
    }
}
于 2013-05-23T18:25:17.730 回答