MVC控制下输出图片、javascript与json格式
/// <summary> /// 输出图片 /// </summary> /// <returns></returns> public ActionResult FileContentResultDemo() { //创建一个文件流 FileStream fs = new FileStream(Server.MapPath(@"/Content/a.jpg"), FileMode.Open, FileAccess.Read); //定义一个buffer数组 byte[] buffer = new byte[Convert.ToInt32(fs.Length)]; fs.Read(buffer, 0, Convert.ToInt32(fs.Length) ); return File(buffer, @"image/gif"); } /// <summary> /// 输出图片 /// </summary> /// <returns></returns> public ActionResult FilePathResultDemo() { //可以将一个jpg格式的图像输出为gif格式 return File(Server.MapPath(@"/Content/a.jpg"), @"image/gif"); } /// <summary> /// 输出图片 /// </summary> /// <returns></returns> public ActionResult FileStreamResultDemo() { FileStream fs = new FileStream(Server.MapPath(@"/Content/a.jpg"), FileMode.Open, FileAccess.Read); return File(fs, @"image/gif"); } /// <summary> /// 输出JavaScript代码 /// </summary> /// <returns></returns> public ActionResult JavaScriptResultDemo() { return JavaScript(@"alert(""Test JavaScriptResultDemo!"")"); } /// <summary> /// 输出Json代码 /// </summary> /// <returns></returns> public ActionResult JsonResultDemo() { var tempObj = new { Controller = "DemoController", Action = "JsonResultDemo" }; return Json(tempObj, JsonRequestBehavior.AllowGet); }