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

Asked

Viewed 247 times

0

I am trying to build a file manager, but I am finding a problem when updating the file. First I do the upload file, and after that, if the user wants to update, I have the option to update, where changes the file, IE, upload and overwrite the already exists. In that, the following error is occurring: o processo não pode acessar o arquivo porque ele está sendo usado por outro processo. Follow the code I’m using for cadastro and edição.

REGISTER

    [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 overwrite the file already registered, so that when changing the old file stay on the server without need.

  • The file is open when you try to perform this process?

  • No, none of that @Diegoaugusto

  • See if it helps you: http://answall.com/questions/71643/close-arquivo-est%C3%A1-being-used-by-another-process

  • In this example ai creates a file, but does not upload @Diegoaugusto

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.