我正在使用 HTML5 画布元素和新的 HTML5 文件 i\o 函数在其上放置多个文件并让它们上传。它工作正常,但现在我需要生成一个新文件名,如果目标目录中没有文件(它是一个 7 位整数)或获取最后上传的文件的名称,将其转换为 int32 并为每个新文件增加一个文件上传到同一目录。这就是GetFileName(dir);
进来。第一个图像总是可以正常上传,但是一旦保存第二个文件并且进程命中 ImageJob.Build(),问题就开始了,我认为这是因为一旦新文件开始写入,GetFile() 方法就会运行同时在行中的第二个文件正在检查最后写入的文件,该文件仍在写入中,这会产生冲突。我该如何解决这个问题,也许我可以以某种方式在 Request.InputStream 数据上使用 foreach 进行迭代,或者实现某种等待进程完成的进程监视?
Update:
我尝试使用 TempData 来存储生成的文件名,并在 TempData 中为所有下一个文件名增加 int 值,它似乎做得更好,可以获取更多图像,但在某些时候仍然会出错。但是 TempData 不是为此,因为它在每次读取后都会被删除,再次重新分配给它并没有帮助。也许我会尝试将它存储在会话中。
该进程无法访问文件“C:\Users\Admin\Documents\Visual Studio 2010\Projects\myproj\myproj\Content\photoAlbums\59\31\9337822.jpg”,因为它正被另一个进程使用。
public PartialViewResult Upload()
{
string fileName = Request.Headers["filename"];
string catid = Request.Headers["catid"];
string pageid = Request.Headers["pageid"];
string albumname = Request.Headers["albumname"];
var dir = "~/Content/photoAlbums/" + catid + "/" + pageid + "/" + (albumname ?? null);
var noex = GetFileName(dir);
var extension = ".jpg";
string thumbFile = noex + "_t" + extension;
fileName = noex + extension;
byte[] file = new byte[Request.ContentLength];
Request.InputStream.Read(file, 0, Request.ContentLength);
string imgdir;
string thumbimgdir;
string imageurl;
if (albumname != null)
{
imgdir = Server.MapPath("~/Content/photoAlbums/" + catid + "/" + pageid + "/" + albumname + "/" + fileName);
thumbimgdir = Server.MapPath("~/Content/photoAlbums/" + catid + "/" + pageid + "/" + albumname + "/" + thumbFile);
imageurl = "/Content/photoAlbums/" + catid + "/" + pageid + "/" + albumname + "/" + thumbFile;
}
else
{
imgdir = Server.MapPath("~/Content/photoAlbums/" + catid + "/" + pageid + "/" + fileName);
thumbimgdir = Server.MapPath("~/Content/photoAlbums/" + catid + "/" + pageid + "/" + thumbFile);
imageurl = "/Content/photoAlbums/" + catid + "/" + pageid + "/" + thumbFile;
}
ImageJob b = new ImageJob(file, imgdir, new ResizeSettings("maxwidth=1024&maxheight=768&format=jpg")); b.CreateParentDirectory = true; b.Build();
ImageJob a = new ImageJob(file, thumbimgdir, new ResizeSettings("w=100&h=100&mode=crop&format=jpg")); a.CreateParentDirectory = true; a.Build();
ViewBag.CatID = catid;
ViewBag.PageID = pageid;
ViewBag.FileName = fileName;
return PartialView("AlbumImage", imageurl);
}
public string GetFileName(string dir)
{
var FullPath = Server.MapPath(dir);
var dinfo = new DirectoryInfo(FullPath);
string FileName;
if (dinfo.Exists)
{
var Filex = dinfo.EnumerateFiles().OrderBy(x => x.Name).LastOrDefault();
FileName = Filex != null ? Path.GetFileNameWithoutExtension(Filex.Name) : null;
if (FileName != null)
{
FileName = FileName.Contains("_t") ? FileName.Substring(0, FileName.Length - 2) : FileName;
int fnum;
Int32.TryParse(FileName, out fnum);
FileName = (fnum + 1).ToString();
if (fnum > 999999) { return FileName; } //Check that TryParse produced valid int
else
{
var random = new Random();
FileName = random.Next(1000000, 9999000).ToString();
}
}
else
{
var random = new Random();
FileName = random.Next(1000000, 9999000).ToString();
}
}
else
{
var random = new Random();
FileName = random.Next(1000000, 9999000).ToString();
}
return FileName;
}