在MVC中删除文件时出错 [英] Error in Deleting file in MVC

查看:82
本文介绍了在MVC中删除文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在System.IO中没有经验,因此当我尝试删除文件时,我收到一条错误消息:无法访问该文件,因为它正被另一个进程使用。请不要发送链接,因为他们都使用FileStream和其他东西,他们使用Dispose方法。在我的案例中,解决方案是什么?以下是代码段:



Hi All,

I am not that experienced in System.IO so when I am trying to delete a file I am getting an error saying: "The process cannot access the file because it is being used by another process". Please don't send links because they all used "FileStream" and other stuffs and they used "Dispose" method. What will be the solution in my case? Below is the code snippet:

foreach (Attachment attachment in viewModel.Attachments)
                                {
                                    string fileName = string.Format("{0}{1}_{2}", folderPath, viewModel.UniqueGuid, attachment.FileName);
                                    if (System.IO.File.Exists(fileName))
                                    {
                                        string encodedFile = Convert.ToBase64String(System.IO.File.ReadAllBytes(fileName));
                                        submitAttachments = Service.Submit(viewModel.<big>SubmitFile</big>(submitApplication.ApplicationGuid, encodedFile, attachment.FileName, attachment.Description));
                                        
                                            
                                        try
                                        {
                                            if (System.IO.File.Exists(fileName))
                                            {
                                                
                                                System.IO.File.Delete(fileName);
                                            }
                                        }
                                        catch
                                        {
                                            throw;
                                        }

                                    }

推荐答案

在mvc中将文件发布到控制器时的所有内容使用这样的结构:



fist of all when posting a file to the controller in mvc use a construct like this:

@using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type="submit" value="OK" />
}





使用这个指定的输入类型并以这种方式指定它可以很好地处理它的控制器方法





using this designated input type and specifying it in this way lends fine into the controller method handling it

public class UploadController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase file)
    {
        if (file != null && file.ContentLength > 0) 
        {
            var fileName = Path.GetFileName(file.FileName);            
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            file.SaveAs(path);
        }        
        return RedirectToAction("Index");        
    }
}





一般来说,你应该小心使用应用程序控制下的地方作为这些东西在非常有限的空间内运行但是因为你可以将它保存在任何地方



最后你没有显示你如何删除以及你保存的地方因此它很可能是你的问题不在你的示例代码中,这使得它更难以帮助。



如果你的命名建议你将文件传递给服务,请将其传递给一个bytearray和一个名称,让你的服务担心文件处理,在这种情况下你开始使用mvc'沙盒'。



Generally you should be careful to use places under the control of the application as these things run in very restricted space but of cause you could save it anywhere

Lastly you're not showing how you delete and where you save so it's very likely that the cause of your problems is not in your sample code, which makes it harder to help.

If like your naming suggestion you're passing the file to a service, pass it as a bytearray and a name and let your service worry about file handling, in which case you're out of the mvc 'sandbox' to begin with.


这篇关于在MVC中删除文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆