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
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.
My program needs to read any type of file from a given directory path and it has to write that information into a byte array.
string combine = Path.Combine(precombine, filename);
string content = System.IO.File.ReadAllText(combine);
This way I can read a text file however I have to read all kind of files such as music or image and write them into a byte array.
Use the File.ReadAllBytes method
byte[] fileContent = System.IO.File.ReadAllBytes(combine);
i have used below code for showing image in image picture box, now i would like add code for opening file (pdf,image,...) after reading from database
int imageID = Convert.ToInt32(imageIDComboBox.Text);
// read image bytes from the database and display in picture box
Byte[] imageByteArray = ProductDB.ReadImage(imageID);
MemoryStream ms = new MemoryStream(imageByteArray);
imagePictureBox.Image = System.Drawing.Image.FromStream(ms);
ms.Close();
i have tried to used below code but it does not recognize Response.
ms.writeto(Response.outputstream)
You need to Save the File somewhere. I suggest you use the GetTempPath method to obtain a temp file name.
after you have saved the file you can open it with the default program of the machine by using the Process class
some pseudo code:
string fileName = "C:\temp\foo.pdf"; //or use Path.GetTempPath()
ms.Write(new StreamWriter(filename)); //you may want to use a using statement for your file stream to ensure the file is closed
Process.Start(filename);
https://msdn.microsoft.com/en-us/library/system.io.path.gettemppath(v=vs.110).aspx
https://www.dotnetperls.com/process
EDIT
It seems my pseudo code does not work correctly ;-) here's another snippet:
Byte[] imageByteArray = ProductDB.ReadImage(imageID);
string fileName = Path.GetTempPath();
File.WriteAllBytes(fileName, imageByteArray);
Process.Start(fileName);
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);
I have a byte array that contains the data of an uploaded file which happens to be a Resume of an employee(.doc file). I did it with the help of the following lines of code
AppSettingsReader rd = new AppSettingsReader();
FileUpload arr = (FileUpload)upresume;
Byte[] arrByte = null;
if (arr.HasFile && arr.PostedFile != null)
{
//To create a PostedFile
HttpPostedFile File = upresume.PostedFile;
//Create byte Array with file len
arrByte = new Byte[File.ContentLength];
//force the control to load data in array
File.InputStream.Read(arrByte, 0, File.ContentLength);
}
Now, I would like to get the contents of the uploaded file(resume) in string format either from the byte array or any other methods.
PS: 'contents' literally refers to the contents of the resume; for example if the resume(uploaded file) contains a word 'programming', I would like to have the same word contained in the string.
Please help me to solve this.
I worked on a similar project a few years ago. Long story short... I ended up reconstructing the file and saving it on the server, then programmatically convert it to pdf, and then index the contents of the pdf, this proved much easier in practice at the time.
Alternatively, if you can restrict resume uploads to docx file format, you can use Microsofts OpenXML library to parse and index the content very easily. But in practict this may cause usability issues for users of the web site.