开发者

FileStream.Write Upload a image, but damaged or corrupted

hi I use FileStream to upload a file to web server, I am using asp.net, the upload works fine when I use Chrome or Firefox, but when I use IE, the file is damaged or corrupted, I could not open it. here is the code

 try
   {

            using (fileStream = new FileStream(saveLoc开发者_Go百科ation, FileMode.Create, FileAccess.Write))
            {
                var length = 4096;
                var bytesRead = 0;
                var buffer = new Byte[length];
                do
                {
                    bytesRead = Request.InputStream.Read(buffer, 0, length);
                    fileStream.Write(buffer, 0, bytesRead);

                } while (bytesRead > 0);
                fileStream.Flush();
            }
        }
        catch
        {
            result = "{\"success\":\"false\"}";
        }
        finally
        {
            if (fileStream != null)

                fileStream.Close();
        }

I already debugged, everything is fine, no exception, file could be save in the server, but just i could not open it? Any help, thanks so much


I would compare the files... Use a tool like winmerge to look and see specifically what is different between the files being uploaded and saved.

once you see the difference you might be able to correct the condition. For example it could be that IE is sending an extra byte at the end of the file... that is making it appear corrupt and you can just chop that off based on the user-agent...

GL


Add the following code after flushing the file

  HttpContext.Current.Response.Close();
  HttpContext.Current.Response.End();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜