我们在控制器类中下载word文档作为字节,我们想将字节数组传递给视图,然后使用Javascript FileSystemObject和Active X Object(Word)打开word docoument。
我不确定如何将字节数组传递给视图。
答案 0 :(得分:0)
您需要创建一个返回return File(byte[]);
的动作方法,并为您编写JavaScript以定位该新网址。
要将字节数组传递给视图,只需在控制器操作方法中使用return View(byte[])
。
答案 1 :(得分: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();
}
}