I have a problem. I have docx file stored as bytes array in database. I need get a url of this file. Url should be like http://my-site.com... but I have no idea how could I reach it. I read many topics with memorystream, filestream, etc. but I still don't understand how I can reach this. I writting in ASP MVC C#.
Asked
Active
Viewed 2,102 times
1
-
I'm thinking you mean the URL is inside the document? Not very efficient, but if you don't understand much about bytes, you can just convert the document into a string and then search using methods yoou know how to use. http://stackoverflow.com/questions/11654562/how-convert-byte-array-to-string – Shannon Holsinger Sep 08 '16 at 12:11
2 Answers
3
For the ASP.NET MVC part you can use the File
method of the controller to return a byte array as file download, like in this example.
public class HomeController : Controller
{
public ActionResult Download(string id)
{
byte[] fileInBytes = GetFileDataFromDatabase(id);
return File(fileInBytes, "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
id + ".docx");
}
private byte[] GetFileDataFromDatabase(string id)
{
// your code to access the data layer
return byteArray;
}
}
The Url would be: http://.../home/download/{someId}

Ralf Bönning
- 14,515
- 5
- 49
- 67
-
Should I be able to download a file after perform Download? I have no results. How get this Url? Sorry for stupid questions, but working wirh files is very difficult for me. – smile Sep 08 '16 at 12:34
-
I just executed my sample with the url (according to the default MVC routing) http://localhost:.../home/download/1234 and the browser prompted me a download dialog. But this is just a sample - please post some code of your controller if it is not working. – Ralf Bönning Sep 08 '16 at 12:45
1
Something like this:
[HttpGet]
[Route("file/{fileId}")]
public HttpResponseMessage GetPdfInvoiceFile(string fileId)
{
var response = Request.CreateResponse();
//read from database
var fileByteArray= ReturnFile(fileId);
if (fileByteArray == null) throw new Exception("No document found");
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(new MemoryStream(fileByteArray));
response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = fileId+ ".docx"
};
response.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.Headers.Add("Content-Transfer-Encoding", "binary");
response.Content.Headers.ContentLength = fileByteArray.Length;
return response;
}
or if you have a Razor Mvc site simply use FileResult: Download file of any type in Asp.Net MVC using FileResult?

Community
- 1
- 1

SilentTremor
- 4,747
- 2
- 21
- 34
-
This implemetation is for a rest api, now trivial explanation: since you are reading the shebang from DB you need to keep it available to the client once the response is leaving the controller (to keep it in memory or on the disk to be consumed by the client), more details: http://stackoverflow.com/questions/8156896/difference-between-memory-stream-and-filestream – SilentTremor Sep 08 '16 at 12:30
-