ASP.NET-MVC – ASP.NET MVC for service TXT is truncated

I am trying to provide a txt file created by the database using actions. The actions are as follows:

public ActionResult ATxt()< br />{
var articulos = _articulosService.ObteTotsArticles();
return File(CatalegATxt.ATxt(articulos), "text/plain");
}

And the CatalegATxt class is:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text ;
using WebDibaelsaMVC.DTOs.Busqueda;

namespace WebDibaelsaMVC.TxtLib
{
public static class CatalegATxt
{
public static Stream ATxt (IEnumerable articles)
{
var stream = new MemoryStream();
var streamWriter = new StreamWriter(stream, Encoding.UTF8);
foreach (ArticuloBusquedaDTO article in articles)
{
streamWriter.WriteLine(article.ToStringFix());
}
stream.Seek(0, SeekOrigin.Begin);
return stream;
}

public static string ToStringFix(this ArticuloBusquedaDTO article)
{
string result = "";
result += article.CodigoArticulo .PadRight(10, '').Substring(0, 10);
result += article.EAN.Trim().PadLeft(13, '0').Substring(0, 13);
result += article.NombreArticulo.PadRight(100, '').Substring(0, 100);
result += article.Marca.PadRight(100,'').Substring(0, 100);< br /> result += article.Familia.PadRight(50, '').Substring(0, 50);
result += article.PrecioCesion.ToStringFix();
result += article.PVP .ToStringFix();
return result;
}

private static string ToStringFix(this double numero)
{
var num = (int)Math .Round(numero * 100, 0);
string result = num.ToString().PadLeft(10, '0');
return result;
}
}
}

It only writes the file line based on what I get from the database. But when I view the file, it looks truncated. The file It is about 8Mb. I also tried to convert to byte[] before returning from ATxt, the result is the same.

Any ideas?

Thanks,

Puyol

Update: I also tried to provide XML from the same content, and it was also truncated. It will not be truncated data ( I think it may be an EOF character), but it is cut off in the middle of the tag…

I Encountered the exact same problem. The text file will always be returned as a truncation.

It suddenly occurred to me that it might be a “flushing” problem, which is indeed the case. The buffer of the writer is at the end of the operation Is not flushed-because there is no block or Close() call-it will automatically flush.

You need to call:

streamWriter.Flush( );

Before MVC takes over the stream.

This is how your method should look like this:

public static Stream ATxt (IEnumerable articles)
{
var stream = new MemoryStream();
var streamWriter = new StreamWriter(stream, Encoding.UTF8);
foreach (ArticuloBusquedaDTO article in articles)
{
streamWriter.WriteLine(article.ToStringFix());
}
// Flush the stream writer buffer
streamWriter.Flush();
stream.Seek(0, SeekOrigin.Begin);
return stream;
}

Hope this helps!

Miroslav

I am trying to use the operation to provide a txt file created by the database. The action is as follows:

public ActionResult ATxt()
{
var articulos = _articulosService.ObteTotsArticles();
return File(CatalegATxt.ATxt(articulos), "text/plain" );
}

The CatalegATxt class is:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using WebDibaelsaMVC.DTOs.Busqueda;

namespace WebDibaelsaMVC.TxtLib
{
public static class CatalegATxt
{
public static Stream ATxt(IEnumerable articles)
{
var stream = new MemoryStream();
var streamWriter = new StreamWriter(stream, Encoding.UTF8);
foreach (ArticuloBusquedaDTO article in articles)
{
streamWriter.WriteLine(article.ToStringFix());
}
stream.Seek(0 , SeekOrigin.Begin);
return stream;
}

public static string ToStringFix(this ArticuloBusquedaDTO article)
{
string result = "";
result += article.CodigoArticulo .PadRight(10, '').Substring(0, 10);
result += article.EAN.Trim().PadLeft(13, '0').Substring(0, 13);
result += article.NombreArticulo.PadRight(100, '').Substring(0, 100);
result += article.Marca.PadRight(100,'').Substring(0, 100);< br /> result += article.Familia.PadRight(50, '').Substring(0, 50);
result += article.PrecioCesion.ToStringFix();
result += article.PVP .ToStringFix();
return result;
}

private static string ToStringFix(this double numero)
{
var num = (int)Math .Round(numero * 100, 0);
string result = num.ToString().PadLeft(10, '0');
return result;
}
}
}

It only writes the file line based on what I get from the database. But when I view the file, it appears to be truncated. The file is approximately It is 8Mb. I also tried to convert to byte[] before returning from ATxt, the result is the same.

Any ideas?

Thanks,

Puyol

Update: I also tried to provide XML from the same content, and it was also truncated. It will not be truncated data ( I thought it might be an EOF character), but it was truncated in the middle of the tag…

I had the exact same problem. The text file will always be Truncated return.

It suddenly occurred to me that it might be a “flushing” problem, which is indeed the case. The buffer of the writer is not flushed at the end of the operation-because there is no block or Close( ) Call-it will refresh automatically.

You need to call:

streamWriter.Flush();

take over in MVC Before streaming.

This is what your method should look like:

public static Stream ATxt(IEnumerable articles)
{
var stream = new MemoryStream();
var streamWriter = new StreamWriter(stream, Encoding.UTF8);
foreach (ArticuloBusquedaDTO article in articles)
{
streamWriter .WriteLine(article.ToStringFix());
}
// Flush the stream writer buffer
streamWriter.Flush();
stream.Seek(0, SeekOrigin.Begin) ;
return stream;
}

Hope this helps!

Miroslav

Leave a Comment

Your email address will not be published.