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);
Related
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 am trying to develop an android application using Xamarin(c#) and parse where user will capture a pic and upload it to parse. I know how to upload a text file but as I am new to this I have no idea how to deal with an image file can anyone please help. This is how I am uploading a text file
byte[] data = System.Text.Encoding.UTF8.GetBytes("This is content of the text file");
ParseFile file = new ParseFile("resume.txt", data);
await file.SaveAsync();
ParseObject gameScore = new ParseObject("GameScore");
gameScore["score"] = 0001;
gameScore["playerName"] = " Bob";
gameScore["e"] = file;
await gameScore.SaveAsync();`
Can anyone please help me with this problem.. thanks.
Parse has an entire section of docs devoted to dealing with files.
// File is in System.IO
byte[] data = File.ReadAllBytes(path_to_your_image);
ParseFile file = new ParseFile(name_of_your_file, data);
await file.SaveAsync();
// link your file object to your Parse object
gameScore["image"] = file;
Update:
The docs specifically say
It's important that you give a name to the file that has a file
extension. This lets Parse figure out the file type and handle it
accordingly. So, if you're storing PNG images, make sure your filename
ends with .png.
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 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 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.