请帮帮我。 我有一个asp.net mvc 3应用程序。我需要从服务器删除文件,但我不能。当我在本地机器上测试我的应用程序 - 删除成功,但当我在Web服务器上运行时,我有错误System.IO.IOException:进程无法访问文件''因为它正被另一个进程使用。
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
...
if (ad.AdPhoto1 != null)
{
System.IO.File.Delete(AppDomain.CurrentDomain.BaseDirectory + "Content/photos/" + ad.AdPhoto1);
}
}
答案 0 :(得分:0)
抱歉不是那么完整的问题,我昨天没有那么多时间...... 该文件是图像(例如.jpg)。我有一个模特:
public class Ad
{
public int AdId { get; set; }
...
[Display(Name = "Photo")]
public string AdPhoto1 { get; set; }
[Display(Name = "Photo 2")]
public string AdPhoto2 { get; set; }
[Display(Name = "Photo 3")]
public string AdPhoto3 { get; set; }
[Display(Name = "Photo 4")]
public string AdPhoto4 { get; set; }
...
}
当我创建一个新的“广告”时,我在服务器上传照片并将其名称写入我的模型。我是怎么做到的:
[HttpPost]
public ActionResult Create(Ad ad, int? Categories, int? SubCategories, IEnumerable<HttpPostedFileBase> fotoUpload)
{
...
if (ModelState.IsValid)
{
int count = 1;
foreach (var file in fotoUpload)
{
Random random = new Random();
int randomItem = random.Next(1, 100);
if (file != null)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "Content/photos/";
string filename = randomItem + count + "Photo" + Path.GetFileName(file.FileName);
if (Request.ContentLength > 1228800)
{
ViewBag.FileSizeError = "Сумарный размер загружаемых вами файлов превышает ограничение в 1,2 МБ";
ModelState.Remove("Categories");
ValuteInitializer();
return View();
}
file.SaveAs(Path.Combine(path, filename));
switch (count)
{
case 1:
ad.AdPhoto1 = filename;
break;
case 2:
ad.AdPhoto2 = filename;
break;
case 3:
ad.AdPhoto3 = filename;
break;
case 4:
ad.AdPhoto4 = filename;
break;
}
}
count++;
}
if (User.Identity.IsAuthenticated)
{
ad.AdAuthor = User.Identity.Name;
ad.AdVisibility = true;
db.SaveChanges();
}
ad.AdData = DateTime.Now;
ad.AdPriority = 3;
ad.CategoryId = Categories;
ad.SubCategoryId = SubCategories;
db.Ads.Add(ad);
db.SaveChanges();
return RedirectToAction("CreateSuccess");
}
return View(ad);
}
我看了很多关于同样问题的帖子,但我找不到解决问题的方法。我想问题可能与流有关,也许。所以,我希望有人可以帮助我...