问题描述:

I have a website that is a gallery so basically admin can upload an image with some information about that image and it'll be shown in the gallery, the problem is that when I upload an image with the size of ~40 KB, it works just fine, but when I upload another image with the size of ~230 KB, it looks it takes for ever to process the matter although the chrome bottom status bar shows the % of the uploading file up to 100% percent but after that it keeps waiting for my server and it never ends... and the uploaded image is like http://www.atrin-gallery.ir/Images/Upload/dalangV.jpg

my code to handle file uploading is as below:

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: I've been using the same function in many of my other websites and they just work fine with even bigger files or images, any ideas?

网友答案:

Remove following lines from your code and use just file.SaveAs(path); to save the file.

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

You don't need to read the Stream and if you try to read using Read method it will advance the position within the stream by the number of bytes read. (Stream.Read Method).

Thanks!

相关阅读:
Top