可以创建一个为FileStreamResult接受3个参数的新构造函数

时间:2011-04-20 16:02:41

标签: asp.net-mvc-3

是否可以为FileStreamResult创建一个带3个参数的构造函数。考虑到我下面的代码,你能告诉我是否需要创建私有方法。如果是,那么需要什么代码。

public FileResult GetImage(int id)
    {
        const string alternativePicturePath = @"/Content/question_mark.jpg";
        MemoryStream stream;
        MemoryStream streaml;

        SubProductCategory4 z = db.SubProductCategory4.Where(k => k.SubProductCategoryFourID == id).FirstOrDefault();

        if ((z != null && z.Image1 != null) && (z != null && z.Image2 != null))
        {

                stream = new MemoryStream(z.Image1);
                streaml = new MemoryStream(z.Image2);
        }

        else
        {
              var path = Server.MapPath(alternativePicturePath);

              foreach (byte item in Request.Files)
              { 
                HttpPostedFileBase file = Request.Files[item];
                if (file.ContentLength == 0)
                {
                    continue;
                }
             }

            stream = new MemoryStream();
            var imagex = new System.Drawing.Bitmap(path);
            imagex.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            stream.Seek(0, SeekOrigin.Begin);

            streaml = new MemoryStream();
            var imagey = new System.Drawing.Bitmap(path);
            imagey.Save(streaml, System.Drawing.Imaging.ImageFormat.Jpeg);
            streaml.Seek(0, SeekOrigin.Begin);
        }



        FileStreamResult newone = new FileStreamResult(); // not sure   
                                  //what additional code is needed here


        return new FileStreamResult(stream, streaml,"image/jpg");
        //'System.Web.Mvc.FileStreamResult' does not contain a 
        // constructor that takes 3 arguments

用于上传图像的FileHandle。我正在使用FileHandle上传图片。

FileHandler.cs

public class FileHandler
{
  public byte[] uploadedFileToByteArray(HttpPostedFileBase file)
  {
    int nFileLen = file.ContentLength;
    byte[] result = new byte[nFileLen];

    file.InputStream.Read(result, 0, nFileLen);

    return result;
 }

 }

1 个答案:

答案 0 :(得分:0)

回答你的直接问题:不,你不能为FileStreamResult创建不同的构造函数,因为该类是MVC框架的一部分(从技术上讲,你可以获取MVC的源代码,进行更改,并自己编译它但我会建议不要这样做。)

如果您能提供有关您尝试做的更多信息,那会更好。你想连接两个流吗?实现最终目标可能有不同的方式。