0
I have a method where after a query, I create a file. txt saving some parameters. My need is to get this . txt, where I will treat this file in a Javascript (Angularjs) to download.
But while trying to get that file, I get a return with the error:
exceptionMessage: "Process cannot access file 'E: nfs-and TEXT API users.txt' because it is being used by another process."
Back end where create and save the file . txt:
public HttpResponseMessage ObterNotas(UsuarioDTO user)
{
var dataInicial = user.CompetenciaInicial;
var dataFinal = user.CompetenciaFinal;
var listaNotas = this.DbContext.ObterNotasRepo(dataInicial, dataFinal);
string path = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory.ToString());
string new_dir = path + "/TEXT/"+ "users.txt";
HttpResponseMessage response = null;
if (!File.Exists(new_dir))
{
response = Request.CreateResponse(HttpStatusCode.Gone);
}
MemoryStream ms = new MemoryStream();
List<string> lista = new List<string>();
using (TextWriter writer = new StreamWriter(new_dir))
{
for (int i = 0; i < listaNotas.Count ; i++)
{
if (i == 0)
{
writer.Write(listaNotas[i].Usuario.CpfCnpj.ToString() + '|' + listaNotas[i].Usuario.RazaoSocial.ToString() + "\r\n");
}
writer.Write(listaNotas[i].NumeroRegistro.ToString() + "\r\n");
}
writer.Flush();
response.Content = new ByteArrayContent(new_dir);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
response.Content.Headers.ContentDisposition.FileName = new_dir;
}
return response;
}
Javascript:
$scope.obterUsuario = function ()
{
$scope.search.prestadorId = authService.authentication.user.codigo;
userService.getUser($scope.search).then(function (result) {
if (result.data.length > 0) {
var data = result.data
console.log(typeof (data));
var headers = result.headers;
headers = headers();
var timeInMs = Date.now();
var contentType = headers['content-type'];
var linkElement = document.createElement('a');
try {
var blob = new Blob([data], { type: contentType });
var url = window.URL.createObjectURL(blob);
linkElement.setAttribute('href', url);
linkElement.setAttribute("download", "teste" + '-' + timeInMs + ".txt");
var clickEvent = new MouseEvent("click",
{
"view": window,
"bubbles": true,
"cancelable": false
});
linkElement.dispatchEvent(clickEvent);
$modalInstance.dismiss('cancel');
} catch (ex) {
console.log(ex);
}
}
});
}
Apparently it is what the message informs that the file is being used by some other process, see if you have not forgotten it opened in another program, VS, Notepad and maybe even some anti-virus
– Leandro Angelo
ms.Close();
, but I didn’t even understand whyMemoryStream
...– Leandro Angelo
@Leandroangelo, it n is open in any program
– user108720
Why don’t you use
File.AppendAllText
? From the code, it seems you have a Java background.– Gabriel
In which line error occurs?
– Leandro Angelo
Enter the code related to the object
response
outside theusing
. The file is only properly closed after theDispose
of the object (which also makes theClose()
ofStreamWriter
).– João Martins
Alias, what is the goal of persisting in a text file the content that is in the database, how will you treat the IO competition of this file in "simultaneous" processes? Another point, a txt with pdf mimetype, will not be a valid pdf file
– Leandro Angelo
@Leandroangelo, I just need to get this file . txt saved in the defined folder. As for the goal, are for other reasons I need to persist in a file.
– user108720
I will return the total I got in the query in the form of . txt
– user108720