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.
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);
Related
I have an excel file, need access, replace parts of the text and download the changed file.
But I can not save the changes, I should always keep the version on the server.
I did several searches, but I can only change the file and save the changes.
I tried to solve with the link below, I managed to search and change the file, but I do not know how to download and stop saving the changes.
Find and replace text in Excel using C#
Thank you very much
Read the file into a memory stream. Do your changes and write it to a byte array. Use the byte array bytesInstream to download and the original file remains unaltered.
byte[] byteArray = File.ReadAllBytes("excelFile.xlsx");
using (MemoryStream ms = new MemoryStream())
{
ms.Write(byteArray, 0, (int)byteArray.Length);
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(ms, true))
{
// Do work here
}
// Convert it to byte array
byte[] bytesInStream = ms.ToArray();
}
I have assumed you are using openxml to make your changes.
I am using ASP.NET and I prefer VB as the language, but I should be able to translate C# for my needs.
I have an array of strings which I would like to send to the browser as individual files for the user to save. In searching the internet, the most common solution to send multiple files to a browser is to zip them up, and send a single zip file.
Towards that end, I need to learn a few things I do not know;
1) What tool/methods (preferably built in to ASP.NET running on IIS7) can I use to create a zip filestream to send to a browser?
2) How do I fool the zip tool into thinking it is getting multiple files from strings in memory? I assume I need to create filestreams, but how do I tell the methods what the file name is, etc.?
If there is an example of doing something substantially similar to what I need available, that would be great. Just point me at it.
Thanks for the help.
The approach could be:
Convert the string into stream
Add data from that stream into the zip file
Write the zip file into response stream
Code example below:
ZipFile zipFile = new ZipFile();
int fileNumber = 1;
foreach(string str in strArray)
{
// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(contents);
MemoryStream stream = new MemoryStream(byteArray);
stream.Seek(0, SeekOrigin.Begin);
//add the string into zip file with a name
zipFile.AddEntry("String" + fileNumber.ToString() + ".txt", "", stream);
}
Response.ClearContent();
Response.ClearHeaders();
Response.AppendHeader("content-disposition", "attachment; filename=strings.zip");
zipFile.Save(Response.OutputStream);
zipFile.Dispose();
I am trying to save a string containing Json syntax to a .txt file on an FTP server.
I tried using this example http://msdn.microsoft.com/en-us/library/ms229715.aspx which worked great.
But this example takes an existing .txt local file and uploads it to the ftp server.
I would like to directly create / update a txt file on the ftp server from a string variable. Without having first to create the txt file locally in my pc.
Your example link is exactly what you need, but you need to get your information from a MemoryStream instead of an existing file.
You can turn a string directly into a Stream with this:
MemoryStream memStr = MemoryStream(UTF8Encoding.Default.GetBytes("asdf"));
However, you can shortcut this more by directly turning your string into a byte array, avoiding the need to make a Stream altogether:
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(yourString);
//and now plug that into your example
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
I have the whole MS Word file itself saved into a byte array.A want to load it the way I would if it was on file system but with the minimal use of Microsoft.Office.Interop.Word because it is very slow when it gets the the .Open(args[]) part.
Try this....
byte[] bte = File.ReadAllBytes("E:\\test.doc"); // Put the Reading file
File.WriteAllBytes(#"E:\\test1.doc", bte); // Same contents you will get in byte[] and that will be save here
There is no supported way to do it right off-the-bat using Interop.Word, as there are no methods supporting byte arrays.
As a viable workaround you can use a temporary file in the following way:
// byte[] fileBytes = getFileBytesFromDB();
var tmpFile = Path.GetTempFileName();
File.WriteAllBytes(tmpFile, fileBytes);
Application app = new word.Application();
Document doc = app.Documents.Open(filePath);
// .. do your stuff here ...
doc.Close();
app.Quit();
byte[] newFileBytes = File.ReadAllBytes(tmpFile);
File.Delete(tmpFile);
Fore additional info, read this post on my blog.
The method public static byte[] ReadAllBytes(
string path
) returns all the file information into a byte array. You dont have to worry about the stream, as the MSDN documentation says:
"Given a file path, this method opens the file, reads the contents of the file into a byte array, and then closes the file."
Check out this link if you want more information
I am using the following C# code to compress a file:
// Open the stream we want to compress
FileStream fs = File.Create(#"C:\Projects\Samples\test\compressed.zip", 0);
// Creates the GZipStream
GZipStream gzip = new GZipStream(fs, CompressionMode.Compress);
// Reading the content to compress
byte[] bytes = File.ReadAllBytes(#"C:\Projects\Samples\samplefile.xml");
// Writing compressed content
gzip.Write(bytes, 0, bytes.Length);
gzip.Close(); // This also closes the FileStream (the underlying stream)
However, when I extract the file from windows explorer the file loses it's extension so instead of samplefile.xml it just becomes samplefile. Same thing happened with .txt file not just .xml file.
Can you help me see what I'm doing wrong?
ok found the problem:
Line 2 has to be as follows:
FileStream fs = File.Create(#"C:\Projects\Samples\test\compressed.xml.zip", 0);
GZipStream doesn't create zip archives. It creates a gzip file, which contains only one file, and doesn't necessarily store a filename at all. Normally you should use the .gz extension to identify a gzip file, and it's conventional to use the entire name of the original file with .gz appended on the end. See also here for more information about gzip format: http://en.wikipedia.org/wiki/Gzip#File_format
If you actually want to create zip archives, you might want to use a library like SharpZipLib: http://www.icsharpcode.net/opensource/sharpziplib/