上传的图片混乱

本文关键字:混乱 | 更新日期: 2023-09-27 18:13:34

我有一个网站,这是一个画廊,所以基本上管理员可以上传一个图像与一些信息的图像,它会在画廊中显示,问题是,当我上传一个图像的大小约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:我在我的许多其他网站上使用了同样的功能,它们对更大的文件或图像也很好,有什么建议吗?

上传的图片混乱

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

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

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

谢谢!