[英]Asp.net MVC retrieve many images from remote server
I user Asp.Net MVC 4 with frameword 4.0 I use this code in my controller for retrieve image from remote server: 我将Asp.Net MVC 4与frameword 4.0结合使用,我在控制器中使用以下代码从远程服务器检索图像:
private List<Imagenes> Galeria(bool Remoto, string Directorio){
string path = string.Empty;
DirectoryInfo dir = null;
List<Imagenes> listImagenes = new List<Imagenes>();
path = Directorio;
dir = new DirectoryInfo(Directorio);
FileInfo[] fileList = dir.GetFiles("*.*", SearchOption.AllDirectories);
var fileQuery = from file in fileList
where ((file.Extension.ToUpper() == ".JPG") ||
(file.Extension.ToUpper() == ".PNG") ||
(file.Extension.ToUpper() == ".GIF") ||
(file.Extension.ToUpper() == ".JPEG"))
orderby file.Name
select file;
foreach (var file in fileQuery)
{
var b = System.IO.File.ReadAllBytes(path + file.Name);
listImagenes.Add(new Imagenes(Convert.ToBase64String(b)));
}
return listImagenes;
}
public class Imagenes
{
public string imagen { get; set; }
public Imagenes(string sImagen)
{
imagen = sImagen;
}
}
This works if the number of images in the directory is 5, if they are more than 5 I get out of memory exception. 如果目录中的图像数为5,则该方法有效,如果它们超过5,则我的内存不足异常。 How I can resolve this problem? 我该如何解决这个问题? if in the remote directory i have more than 30 images 如果在远程目录中,我有超过30张图像
To avoid retrieving all files from the get-go, try this: 为了避免从一开始就检索所有文件,请尝试以下操作:
FileInfo[] files = dir.GetFiles("*", SearchOption.AllDirectories).
Where( f =>
f.Extension.ToUpper() == ".JPG" ||
f.Extension.ToUpper() == ".PNG" ||
f.Extension.ToUpper() == ".GIF" ||
f.Extension.ToUpper() == ".JPEG").
ToArray<FileInfo>();
Furthermore, you should clear your Byte array before your next file iteration: 此外,您应该在下一次文件迭代之前清除Byte数组:
Array.Resize<byte>(ref b, 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.