how to export the text file in mvc? - c#

I generate the text file in my code using the stream writer using mvc format for web application . But my text file not download the my page? my sample code is below:
// file have the all values in stream writerformat
return File(file, "text/plain", "Export.txt");

try adding this before your return:
var cd = new System.Net.Mime.ContentDisposition
{
FileName = "Export.txt",
Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(file, "text/plain");

You can call a method that will give you your file stream like this:
public FileStreamResult DownloadFile()
{
//
// This part is where you build your file
//
// file should be a Stream
return new FileStreamResult(file, "text/plain")
{
FileDownloadName = "optional_name_that_you_can_give_your_file"
};
}

Related

How can I read a file which will be upload from a form in .Net Core API?

I create a method in my .Net Core API which will upload a file.
[HttpPost]
public async Task<IActionResult> ReadFile(IFormFile file)
{
return BadRequest(file);
}
I do a return BadRequest(file) in order to read what it send me on postman.
The result is this :
{
"contentDisposition": "form-data; name=\"file\"; filename=\"data.dat\"",
"contentType": "application/octet-stream",
"headers": {
"Content-Disposition": [
"form-data; name=\"file\"; filename=\"data.dat\""
],
"Content-Type": [
"application/octet-stream"
]
},
"length": 200,
"name": "file",
"fileName": "data.dat"
}
I see on Microsoft documentation this :
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// Read the stream to a string, and write the string to the console.
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
But the user will have to choose a file to read in it, the application won't have to go in a folder and read a file.
It is possible to do this ? And can I have some link to help me to do this ?
Update
I want to the method ReadFile read the content of my file which will be upload thinks to a form.
So I will have a string which will have the content of my file and after that I will can all I wanted to do in this file.
For example I have a file and in this file it is wrote LESSON, with the method ReadFile I will get the word lesson in a string.
The file will be bound to your IFormFile param. You can access the stream via:
using (var stream = file.OpenReadStream())
{
// do something with stream
}
If you want to read it as a string, you'll need an instance of StreamReader:
string fileContents;
using (var stream = file.OpenReadStream())
using (var reader = new StreamReader(stream))
{
fileContents = await reader.ReadToEndAsync();
}
In you Controller :
Check if IFormFile file contains something
Check if the file's extension is the one you are looking for (.dat)
Check if the file's Mime type is correct to avoid attacks
Then, if it is all right, call a Service class to read your file.
In your Service, you can do something like following :
var result = new StringBuilder();
using (var reader = new StreamReader(file.OpenReadStream()))
{
while (reader.Peek() >= 0)
result.AppendLine(await reader.ReadLineAsync());
}
return result.ToString();
Hope it helps.

Converting StreamReader back to pdf

I am trying to convert pdf stream back to pdf file, then save it on my server.
The function GetHTTPRequest gets pdf url and returns the pdf url stream string.
I need to convert this stream to pdf file.
My Code:
public ActionResult Html(string strUrl)
{
string xhr;
xhr = GetHTTPRequest(strUrl, "GET");
// make pdf file fron xhr
return View("Index");
}
[ChildActionOnly]
public static string GetHTTPRequest(string RequestUrl, string RequestMethod)
{
try
{
HttpWebRequest r = (HttpWebRequest)WebRequest.Create(RequestUrl);
r.Method = RequestMethod;
HttpWebResponse res = (HttpWebResponse)r.GetResponse();
Stream sr = res.GetResponseStream();
StreamReader sre = new StreamReader(sr);
string s = sre.ReadToEnd();
return s;
}
catch (Exception)
{
return string.Empty;
}
}
This is only 1 line from what xhr contains:
"%PDF-1.3\n%�쏢\n5 0 obj\n<>\nstream\nx��\k���V �DB��rq��\r}��_�4U[��\nTҮU�~�ِBI h����>gl����n�N���Y{.�~�33�;�h����뷶.��W7�n����ͭ;[������[կ��?TR��T�_l�6� �O�e�ll��\m\b�����T����F�����V��n���6h嫍��Hk��l�R��O6ô3�7O5�:�.�i<�:���3��S�j��V�O��\b�~&\r�S93�l�ѭ�Q�z�v�cF)���\r��g��m�v����Z���\a���;{�i��Bq�7��^xK�7�U��P?��z>����]���F\b1�X�ec��/4�ji��/К��&#�EjF)�g�D�\v)��_ �\r|��XSEcu�\nILz��H|�ZL�a�wO\af^m6�NzS_lb$�Vx\rB�VG��_���44�v���������w������4J����Q�z���7�刭��+�a�|�7�Z����&��ٕQ��LsE��c�t뜁������)�ad���ӷ2��inSU��-��\a��\fā4ʹ�v1�w�ֽ����)�����S0�����2M�~�S�;�Ԩ2�\a|�����'K�0[\"{��F��5��2�ٱJ[��ӑ ��F�����3��X�s'\b9�[�&�Et��A\"�����N���������)7=�p�T�v.�!�q\"��H����M-i�,��TA��P����tV{]V=z����\rV����T��o�c�s�������[w*i�g��R�EZ�Λ.JQ}�\r:�w)�Ȕڣ�����{�)e%�(GF3�H8�G�Ԣ5)G^u�C�F�ÂQ\v�A=I\"�G\"�#�i�X�3d�����P;%=r���8#\he<�IAv��\a������D[ؕ]���7X��!��\b#B�Y$a5\b \b��T�.���Կ,\n�)1�V�\0N�z�$Pȡ����%
Can someone please put some light on it and solve it for me?
I never used stream libary before.
Thanks.
PDF is a binary file format. Don't use a string it doesn't make sense. What you are doing is eqivalent to trying to open a pdf file with NotePad. You should save the pdf file as file.
This is how you would download the file.
string resourceUrl = "http://www.google.com/file.pdf";
string fileName = downloadedFile.pdf;
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(resourceUrl,fileName);
If you need to read file.pdf as a string that's a different matter. You will need to look for a pdf parser. iTextSharp is not a bad choice for a start.

Issues when downloading PDF file from MVC application

I have created a function where a user can download a pdf file from my webpage. The file is stored in a databse and is requested from a webapi. The return value of the webapi is a byte[].
My issue here is that when i run the web application on my local iis this function runs without any errors. I get the pdf file and it is downloaded correctly on my machine. But when i deploy the web application to my Test server this code generates either RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION in chrome with some of the files where as other files are downloaded to the machine but when i try to open the pdf file i get: could not load the pdf file.
This happens with both chrome and IE.
This is my code:
[HttpGet]
[DoNotChangeCacheSettings]
public virtual FileResult DownloadTranslationFile(Guid id)
{
Guid assessmentTemplateId = id;
File translationFile = Services.GetFileContent(assessmentTemplateId);
var fileName = HttpUtility.UrlPathEncode(translationFile.FileName);
this.HttpContext.Response.Headers.Add("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
var result = File(translationFile.FileContent.Content, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);
return result;
}
I have been trying to fix this issue for 2 days now but i simply cant figure out what the issue is. Hope you guys can help. Thanks.
You don't need to use Content-Disposition. .Net will add it for you. From the docs.
The fileDownloadName parameter is used to generate the
content-disposition header. The result object that is prepared by this
method is written to the response by the ASP.NET MVC framework when
the object is executed. The MediaTypeNames class can be used to get
the MIME type for a specific file name extension.
I tend to use the Stream-overload:
[HttpGet]
[DoNotChangeCacheSettings]
public virtual FileResult DownloadTranslationFile(Guid id)
{
Guid assessmentTemplateId = id;
File translationFile = Services.GetFileContent(assessmentTemplateId);
var fileName = HttpUtility.UrlPathEncode(translationFile.FileName);
var stream = = new MemoryStream(translationFile.FileContent.Content);
return File(stream, "application/pdf", fileName);
}
But you can use the byte[] as well:
[HttpGet]
[DoNotChangeCacheSettings]
public virtual FileResult DownloadTranslationFile(Guid id)
{
Guid assessmentTemplateId = id;
File translationFile = Services.GetFileContent(assessmentTemplateId);
var fileName = HttpUtility.UrlPathEncode(translationFile.FileName);
return File(translationFile.FileContent.Content, "application/pdf", fileName);
}
EDIT:
If you got an error when opening the PDF you can ensure that the web browser is doing the right thing by manually saving the PDF from code as well. If that file has errors as well you're probably generating an incorrect byte[].
[HttpGet]
[DoNotChangeCacheSettings]
public virtual FileResult DownloadTranslationFile(Guid id)
{
Guid assessmentTemplateId = id;
File translationFile = Services.GetFileContent(assessmentTemplateId);
var fileName = HttpUtility.UrlPathEncode(translationFile.FileName);
var stream = = new MemoryStream(translationFile.FileContent.Content);
// Code for debugging
var tempDir = "C:\\temp"; // Make sure app pool can write here.
var path = Path.Combine(tempDir, fileName); // Possibly add extension here.
using (var fileStream = File.Create(path))
{
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(fileStream);
}
stream.Seek(0, SeekOrigin.Begin);
// Return to client.
return File(stream, "application/pdf", fileName);
}

PDF is not rendered on all the browsers using Asp.net

I am generating dynamic .pdf file using asp.net, on some browsers e.g firefox displays the pdf as expected but on I.E & Safari its giving a "Save As" rather than displaying it in the browser. I have used an iFrame for the same. Also to display pdf file is it mandate that the client should have adobe reader or some plugin to display the pdf?
protected void bttnpdf_Click(object sender, EventArgs e)
{
string FilePath = Server.MapPath("sample.pdf");
WebClient User = new WebClient();
Byte[] FileBuffer = User.DownloadData(FilePath);
if (FileBuffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", FileBuffer.Length.ToString());
Response.BinaryWrite(FileBuffer);
}
}
Any help would be appreciated.
Thanks in advance
in aspx file :
<div>
<iframe id="myFrame" runat="server" style="width:600px; height:500px;" frameborder="0"></iframe>
</div>
in cs file :
myFrame.src="yourPDFPath"
for example
myFrame.Src = "http://docs.google.com/gview?url=http://path.com/to/your/pdf.pdf&embedded=true";
When I have done this I have also added a content disposition. In the code (can't share it all as it is production code) there is a function that finds a record in the database and returns a FileHandle.
The function returns a FileHandle:
FileStream stream = new FileStream(filePath, FileMode.Open);
var fileHandle = new FileHandle
{
FileStream = stream,
ContentType = "application/pdf",
Filename = fileDownloadName
};
where filePath is the path to the file (e.g. Server.MapPath("sample.pdf") ). In the controller action I have:
var cd = new System.Net.Mime.ContentDisposition
{
// for example foo.bak
FileName = fileHandle.Filename,
// always prompt the user for downloading, set to true if you want
// the browser to try to show the file inline
Inline = true,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(fileHandle.FileStream, fileHandle.ContentType);
Add below line before the line Response.BinaryWrite(FileBuffer);.
Response.AddHeader("Content-Disposition", "inline");
add this header to reponse , so that PDF files will opened in browser itself.

Corrupt File when streaming from HTTP

I’m having an issue with streaming a file through the HTTP Response. No matter what I do, it comes out corrupted!
The background is that I need to send a generated XLS file (I'm using NPOI). I know that the generated file is fine, because if I save it directly to the disk with a FileStream, I can open it and there are no problems! However… when I try to stream that file through HTTP, it comes out corrupted (I’ve tried three different methods, shown below…). To add on top of that, it’s not only the XLS file that gets corrupted, it’s ALL files that I load (I’ve tried jpg, png, and txt files). Whenever I send them through HTTP, it gets corrupted.Anyways, here’s what I’ve tried:
I’ve tried manually constructing an HTTP response:
Export export = new Export(header, data);
MemoryStream stream = export.GetXLSStream("test"); // This generates a memory stream of the XLS file
// Writing that stream to a file works! This file opens just fine
var fs = new FileStream(#"C:\export.xls", FileMode.Create, System.IO.FileAccess.Write);
stream.WriteTo(fs);
// However, this doesn't!
Response.ClearContent();
Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
Response.AddHeader("Content-Disposition", "attachment; filename=export.xls");
Response.AddHeader("Content-Type", "application/vnd.ms-excel");
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.BinaryWrite(stream.ToArray());
Response.End();
return null;
I’ve tried using the FileStreamResult:
Export export = new Export(header, data);
MemoryStream stream = export.GetXLSStream("test"); // This generates a memory stream of the XLS file
return File(stream, "application/vnd.ms-excel", "export.xsl");
I’ve tried using the FileContentResult:
Export export = new Export(header, data);
MemoryStream stream = export.GetXLSStream("test"); // This generates a memory stream of the XLS file
return File(stream.ToArray(), "application/vnd.ms-excel", "export.xsl");
I’ve tried using a FilePathResult:
Export export = new Export(header, data);
MemoryStream stream = export.GetXLSStream("test"); // This generates a memory stream of the XLS file
var fs = new FileStream(#"C:\export.xls", FileMode.Create, System.IO.FileAccess.Write);
stream.WriteTo(fs);
fs.Close();
return File(#"C:\export.xls", "application/vnd.ms-excel", "export.xsl");
And I’ve tried loading random files like:
return File(#"C:\test.jpg", "image/jpeg", "test.jpg");
Doing a MD5 or CRC check also shows me that the file I get through HTTP is not the same as the original file (even though they have the exact same amount of bytes).
Here is the code that works for me:
public MyController : Controller {
public ActionResult GetFile()
{
// ...
return File(stream, "application/octet-stream", "file.xls");
}
}
I do it in MVC with NPOI 1.2.5, .NET 4.0 in following way:
I have custom ActionResult class with following method:
public override sealed void ExecuteResult(ControllerContext context)
{
IWorkbook workbook = XlsData.CreateTestWorkbook().Workbook;
HttpResponseBase response = context.HttpContext.Response;
response.Clear();
response.ContentType = "application/vnd.ms-excel";
response.Headers.Add("content-disposition", "attachment; filename=Test.xls");
using (var ms = new MemoryStream())
{
workbook.Write(ms);
ms.WriteTo(response.OutputStream);
}
response.End();
}
where XlsData.CreateTestWorkbook() is some class which creates me NPOI sample workbook.
Then in my Controller method I simply return my custom ActionResult
My controller method has a [HttpGet] attribute and is called from
client side with a html link download button.
Looks like it was a Whitespace filter I had applied at the controller's root to make the output all a single line. It shouldn't have been applied to anything but an html response, but I've changed the code so that it doesn't run at all for response like this.
Try this method, it works fine for me
using (var fileData = new FileStream(filename, FileMode.Create))
{
workbook.Write(fileData);
}
using (var exportData = new MemoryStream())
{
workbook.Write(exportData);
string saveAsFileName = string.Format("MembershipExport-{0:d}.xls", DateTime.Now).Replace("/", "-");
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
Response.Clear();
Response.BinaryWrite(exportData.GetBuffer());
Response.End();
}
MemoryStream outStream = (MemoryStream)generateFilte.Generate();
outStream.Flush(); //Always catches me out
outStream.Position = 0;
return new FileStreamResult(outStream, "application/vnd.ms-excel");

Categories