2014-07-18 55 views
0

我有一个网站是一个画廊,所以基本上管理员可以上传一张图片和关于该图片的一些信息,它会显示在图库中,问题是当我上传图片时大小约为40 KB,但它工作得很好,但是当我上传另一张大小为〜230 KB的图像时,尽管Chrome底部状态栏显示上传文件的百分比高达100%%,但此后它一直在等待我的服务器,它永远不会结束......和上传的图片是一样http://www.atrin-gallery.ir/Images/Upload/dalangV.jpg已上传的图像被搞砸了

我的代码来处理文件上传是如下:

if (Request != null) 
     { 
      try 
      { 
       HttpPostedFileBase file = Request.Files["image"]; 

       if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName)) 
       { 
        string subPath = "~/Images/Upload"; // your code goes here 

        bool isExists = System.IO.Directory.Exists(Server.MapPath(subPath)); 

        if (!isExists) 
        { 
         System.IO.Directory.CreateDirectory(Server.MapPath(subPath)); 
        } 

        string fileName = Path.GetFileName(file.FileName); 
        fileName = fileName.Replace(" ", ""); 
        var path = Path.Combine(Server.MapPath(subPath), fileName); 
        string fileContentType = file.ContentType; 
        byte[] fileBytes = new byte[file.ContentLength]; 
        file.InputStream.Read(fileBytes, 0, file.ContentLength); 
        file.SaveAs(path); 
       } 
      } 
      catch (Exception e) 
      { 

      } 
     } 

P.S:我在许多其他网站上都使用过相同的功能,他们可以在更大的文件或图像,任何想法中工作良好?

+0

为什么你将ContentLength转换为int时,它已经是int了? –

+0

是好点,但仍然没有帮助主要问题...无论如何去除Convert.ToInt32。 –

+0

在像emacs这样的原始数据查看器中打开图像。比较不好的形象和良好的形象。最后有一大堆垃圾角色吗?在中间?如果你重新上传,问题总是在同一个地方,还是有所不同? –

回答

0

从代码中删除以下行,并使用file.SaveAs(path);来保存该文件。

string fileContentType = file.ContentType; 
byte[] fileBytes = new byte[file.ContentLength]; 
file.InputStream.Read(fileBytes, 0, file.ContentLength); 

你并不需要阅读Stream,如果你尝试使用Read方法来读它会读取的字节数推进流中的位置。 (Stream.Read Method)。

谢谢!