0

In Microsoft CRM we have an attachment that should be fetched and downloaded. So I have a byte array that represents the fetched file:

byte[] fileContent = Convert.FromBase64String(query.DocumentBody);

If I use this code, of course it can be downloaded but the file path should be hardcoded (like C:/<folder name>/) and I don't want it like that.

using (FileStream fileStream = new FileStream(path + query.FileName, FileMode.OpenOrCreate))
{
    byte[] fileContent = Convert.FromBase64String(query.DocumentBody);
    fileStream.Write(fileContent, 0, fileContent.Length);

    //Response.OutputStream.WriteByte(fileContent);
}

How can I download the file from a byte array? I've tried searching for ways but it all needs a file path, and I can't provide that file path since the object is a byte array.

Raptor
  • 53,206
  • 45
  • 230
  • 366
Dustine Tolete
  • 461
  • 1
  • 7
  • 19
  • There are many question similar to this on like http://stackoverflow.com/questions/12140836/downloading-large-files-saved-in-database - use following search to find more http://stackoverflow.com/search?q=[c%23]+download+database+disposition – Alexei Levenkov Jan 30 '14 at 03:25
  • i reviewed those links, and like what i said in the post, i don't want to provide a path to where the download should be saved. i want it by default to be saved on the default download folder. right now i'm doing it in the hardcoded way where it will be saved on the drive C:/ and it poses security and access issues. – Dustine Tolete Jan 30 '14 at 03:29
  • I guess I'm missing your question completely. What kind of app/site you are writing, where do you want the file to be stored at particular path (server / machine with browser/ somewhere else)? – Alexei Levenkov Jan 30 '14 at 03:32
  • i want it (the file from byte array) to be stored to the default download folder of the client machine who access the site. – Dustine Tolete Jan 30 '14 at 03:34
  • you can't control that. There is nothing you can do about it except providing recommended file *name* with "content-disposition" header. – Alexei Levenkov Jan 30 '14 at 03:39
  • yes, i've tried using the content-disposition header, but the file downloaded can't be opened with a file size of 0 bytes. meaning the file content of the byte array is not passed to the downloaded file. – Dustine Tolete Jan 30 '14 at 03:41

1 Answers1

1

I'm not sure what exactly is your problem, but following should write byte array to output stream. You may need "content-disposition" header for file name and "content-type" to let browser offer "download" instead of trying to open directly:

Response.OutputStream..Write(fileContent , 0, fileContent .Length);
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • yea, i'm trying this one now. is my code right ? Response.ContentType = query.Type; Response.AddHeader("content-disposition", "attachment;filename=" + query.FileName); Response.OutputStream.Write(fileContent, 0, fileContent.Length); Response.End(); Response.Flush(); Response.Close(); – Dustine Tolete Jan 30 '14 at 03:53
  • it really takes so long in compiling and running this project. will update you if this code works. thanks dude. – Dustine Tolete Jan 30 '14 at 03:57