Upload - the process can not access the file because it is being used by another process

0

I'm trying to mount a file manager, but I'm having trouble updating the file. First I make the upload of the file, and after that, if the user wants to update, I have the option of updating, where it changes the file, that is, it uploads and overrides the already exists. In this, it is giving the following error: o processo não pode acessar o arquivo porque ele está sendo usado por outro processo . Here is the code I'm using for cadastro and edição .

REGISTRATION

    [HttpPost]
    public ActionResult Cadastrar(Arquivo arquivo, HttpPostedFileBase upload)
    {
        using (var ts = _db.Database.BeginTransaction())
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var nome = CriptografiaUtil.Outros(DateTime.Now.ToString()) + Path.GetExtension(upload.FileName);
                    var urlUpload = Path.Combine(Server.MapPath(DiretorioUpload.Arquivo()), nome);
                    upload.SaveAs(urlUpload);
                    arquivo.Url = DiretorioUpload.Arquivo() + nome;
                    ArquivoDAO.Cadastrar(arquivo, ref _db);
                    ts.Commit();
                    return RedirectToAction("Index");
                }
                return View(arquivo);
            }
            catch (Exception ex)
            {
                ts.Rollback();
                return View();
            }
        }
    }

EDITION

[HttpPost]
public ActionResult Atualizar(Arquivo arquivo, HttpPostedFileBase upload)
{
    using (var ts = _db.Database.BeginTransaction())
    {
        try
        {
            if (ModelState.IsValid)
            {
                if (upload != null)
                {
                    var arquivoCadastrado = ArquivoDAO.Buscar(arquivo.Id, ref _db);
                    var nomeArquivoCad = Path.GetFileName(arquivo.Url);
                    if (nomeArquivoCad != null)
                    {
                        var urlUpload = Path.Combine(Server.MapPath(DiretorioUpload.Arquivo()), nomeArquivoCad);
                        upload.SaveAs(urlUpload);
                    }
                }
                ArquivoDAO.Editar(arquivo, ref _db);
                ts.Commit();
                return RedirectToAction("Index");
            }
            return View(arquivo);
        }
        catch (Exception ex)
        {
            ts.Rollback();
            return View();
        }
    }
}

As you can see, in the edition I just overlay the already registered file, so that when changing the old file it stays on the server unnecessarily.

    
asked by anonymous 15.07.2016 / 14:20

0 answers