Is there a way to take a byte[] and open the file rather than having to write it to disk and delete it when finished? They will be pdf files.
Update 2
if (e.Result is byte[])
{
byte[] message = (byte[])e.Result;
using (MemoryStream stream = new MemoryStream(message))
{
stream.Position = 0;
wbMessage.NavigateToStream(stream);
}
}
I scrapped this idea and used Adobe's COM object to display my PDFs
MemoryStream has constructor that takes byte[]
Related
I am trying to read an IFormFile received from a HTTP POST request like this:
public async Task<ActionResult> UploadDocument([FromForm]DataWrapper data)
{
IFormFile file = data.File;
string fileName = file.FileName;
long length = file.Length;
if (length < 0)
return BadRequest();
using FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate);
byte[] bytes = new byte[length];
fileStream.Read(bytes, 0, (int)file.Length);
...
}
but something is wrong, after this line executes:
fileStream.Read(bytes, 0, (int)file.Length);
all of the elements of bytes are zero.
Also, the file with the same name is created in my Visual Studio project, which I would prefer not to happen.
You can't open an IFormFile the same way you would a file on disk. You'll have to use IFormFile.OpenReadStream() instead. Docs here
public async Task<ActionResult> UploadDocument([FromForm]DataWrapper data)
{
IFormFile file = data.File;
long length = file.Length;
if (length < 0)
return BadRequest();
using var fileStream = file.OpenReadStream();
byte[] bytes = new byte[length];
fileStream.Read(bytes, 0, (int)file.Length);
}
The reason that fileStream.Read(bytes, 0, (int)file.Length); appears to be empty is, because it is. The IFormFile.Filename is the name of the file given by the request and doesn't exist on disk.
Your code's intent seems to be to write to a FileStream, not a byte buffer. What it actually does though, is create a new empty file and read from it into an already cleared buffer. The uploaded file is never used.
Writing to a file
If you really want to save the file, you can use CopyTo :
using(var stream = File.Create(Path.Combine(folder_I_Really_Want,file.FileName))
{
file.CopyTo(stream);
}
If you want to read from the uploaded file into a buffer without saving to disk, use a MemoryStream. That's just a Stream API buffer over a byte[] buffer. You don't have to specify the size but that reduces reallocations as the internal buffer grows.
Reading into byte[]
Reading into a byte[] through MemoryStream is essentially the same :
var stream = new MemoryStream(file.Length);
file.CopyTo(stream);
var bytes=stream.ToArray();
The problem is that you are opening a new filestream based on the file name in your model which will be the name of the file that the user selected when uploading. Your code will create a new empty file with that name, which is why you are seeing the file in your file system. Your code is then reading the bytes from that file which is empty.
You need to use IFormFile.OpenReadStream method or one of the CopyTo methods to get the actual data from the stream.
You then write that data to your file on your file system with name you want.
var filename ="[Enter or create name for your file here]";
using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate))
//Create the file in your file system with the name you want.
{
using (MemoryStream ms = new MemoryStream())
{
//Copy the uploaded file data to a memory stream
file.CopyTo(ms);
//Now write the data in the memory stream to the new file
fs.Write(ms.ToArray());
}
}
What I am trying to accomplish is allowing my user to upload a PDF. I will then convert that to an Image and get the Images byte array. Below is what I have so far.
PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor();
using (MemoryStream ms = new MemoryStream(e.UploadedFile.FileBytes))
{
pdfDocumentProcessor.LoadDocument(ms);
for (int i = 1; i <= pdfDocumentProcessor.Document.Pages.Count; i++)
{
Bitmap image = pdfDocumentProcessor.CreateBitmap(i, 1000);
try
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
}
catch (Exception error)
{
string message = error.Message;
}
}
When I try to save the Image to the memory stream I am getting the error "A generic error occurred in GDI+" I believe this has something to do with me not specifying a path for the image to be saved to, but I could be mistaken.
I want to convert the PDF to and Image, then get the byte array of the image, and save that to the database. I really don't want to save the image to a specified path.
PDFDocumentProcessor is a DevExpress class that pulls in the PDF and also will give me the PDF's byte array, but I just can't seem to find a way past the save error to retrieve an Image byte array
Any help is appreciated thank you
The issue is likely caused by you trying to re-use the same MemoryStream that is holding the input file bytes. You should create a new memory stream to save to.
I don't have access to devexpress but I grabbed another Nuget package that i am associated with https://www.nuget.org/packages/Leadtools.Pdf/ and tested it and this code works to save the PDF to a PNG memorystream:
using (var ms = new MemoryStream(fileBytes))
using (var codecs = new RasterCodecs())
{
codecs.Options.Load.AllPages = true;
using (var rasterImage = codecs.Load(ms))
using (var outputStream = new MemoryStream())
codecs.Save(rasterImage, outputStream, RasterImageFormat.Png, 0);
}
I'm scaning a Pdf417 barcode which returns me a byte[] array. The DataString itself is a cryptic value like face to keyboard multiple times and fast. So I'm guessing it could be a zip file which is stored in the barcode. In the zip file there should be a xml file.
I had different attempts so far to convert my byte[] array to a valid zip file. In the end I was never able to open said zip file.
The barcodes are created by certified software solutions, so the barcode is not the problem for sure.
I can't be the only one who has had this problem, right?
Output when reading a barcode with dummy data in it:
"\0\0\0\vz\0\u0002B\u0001\u0002PK\u0003\u0004\n\0\0\0\b\0 E\u0081Q|\u0015\u00163Î\u0001\0\0\u0004\u0004\0\0\u0004\0\0\0txab}SMo£0\u0010½ï¯°|\a\u001bª¤Ý\u0015Påc\u0093Fê&\b²9ôÆÂ$ \r¦²M\u0093üûNø\b$Që\u008bí7ï½\u0019{lçù\u0098ïÉ\aH\u0095\u0015Â¥\u0096É)\u0001\u0011\u0017I&v.ý»\u009e\u0019OôÙsÖ\u0004iB¹4Õúý\u0017c\u0087ÃÁT\u0087L©\u0004b3N\u0099\u008aSÈ#¦\u0012fs>ä6·X\u0018í#y\u009aB\u008cS¤Ñ}}\u001c\u008d)\t\u0017S\u0097ÎV+\\u009dÔ¦^z?\b\u000egRäï\u00918\u0091\u0097À\b&Æ2ÊÁ¥/\u0091Pä\u000fd ÉhNÉÛÂwé\u0013ç\u009c²Fäcé\u0085XLÉk¤´¨4\u0015\u009d\u0092Y&[äìÒ\u0017×ÚJ\u001fn\u008cQh,¥÷8\u0018\u009a\u0016\u000eÓÆa>ütØ%Tgbmªf\u001fö\0\u0094\u0015I\aTàV\u0016¹gãe\u0018|`pËa\u0015pÍ)\u0085Îö5ɲ\u008d\a$ÕHgÍn½\u009d\u0005ö'\ao\u0080'á&ç\u000ek\u0080\u008e1\u0093Ø>\u0018\u0083\u0080m¦\u0015ëEV:\u0005Ù\u0006njYÃQ{ãB\u0094ÊaÕú:\u001c\u0096¹g]r\u009ew½"¿ðuæ²Pª©ox\u0011÷Ñ\u008e;ÞÌ\u008dWß7&\u0085Ð2ûW\u009e\u001fÍM\r\u0001ìJ|Oxa\u008dS\v\ÓüRÆi¤ ª·â]\u0090^Íßçs\u0096 Û\u009b~lm:¬ãM!)ã³v¤Ã\u0002Óô²Þ\u0087:Ù$\u008dä\u000eTPî\u0081ÝÃ7\aú½Ý\u0002\u001a}\QÙ\u001d·nï×Ý\u000fð\u0093Êÿí×aø\u0082±ÓÞ'PK\u0001\u0002\u0014\0\n\0\0\0\b\0 E\u0081Q|\u0015\u00163Î\u0001\0\0\u0004\u0004\0\0\u0004\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0txabPK\u0005\u0006\0\0\0\0\u0001\0\u0001\02\0\0\0ð\u0001\0\0\0\0"
Dont pay too much attention to this function, at this stage I'm probably trying too much to get to a solution. This is just one of many test voids of mine.
public void Test(byte[] bytes)
{
byte[] zipBytes;
using (var memoryStream = new MemoryStream())
{
using (var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, leaveOpen: true))
{
var zipEntry = zipArchive.CreateEntry("test");
using (Stream entryStream = zipEntry.Open())
{
entryStream.Write(bytes, 0, bytes.Length);
}
}
zipBytes = memoryStream.ToArray();
}
using (var fileStream = new FileStream(#"C:\BarcodeReaderTesting\test.zip", FileMode.OpenOrCreate))
{
fileStream.Write(zipBytes, 0, zipBytes.Length);
}
}
Any tips on this topic?
I'm trying to "pack" a dll file into a file that contains other resources. I'm reading the bytes of the DLL and writnig them back in the "package" file, but when I unpack everything, the DLL is corruped.
I can see that I'm writing the "package" file incorrectly ( input is 8kb - output is 9kb and dll is not working ) and I can't seem to get why this is happening.
I read the bytes from the input file, put them into memorystream via binarywriter and then write them to the "package" file using File.WriteAllBytes.
byte[] bytes = File.ReadAllBytes(fileDest[i]);
using (MemoryStream ms = new MemoryStream())
{
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(count); //int32
bw.Write(version); //int32
for (int i = 0; i < count; i++)
{
bw.Write(lengthofbytes);
bw.Write(someinfo); // string
bw.Write(filecontents); // bytes
}
byte[] bytes = ms.ToArray();
File.WriteAllBytes("some.dll", bytes);
}
What I'm trying to do is combine a bunch of files in one file and then "unpack" them where they need to be. Unfortunately one of the files has to be a .DLL
I have a scenario where by I want to zip an email attachment using SharpZipLib. Then the end user will open the attachment and will unzip the attached file.
Will the file originally zipped file using SharpZipLib be easily unzipped by other programs for my end user?
It depends on how you use SharpZipLib. There is more than one way to compress the data with this library.
Here is example of method that will create a zip file that you will be able to open in pretty much any zip aware application:
private static byte[] CreateZip(byte[] fileBytes, string fileName)
{
using (var memoryStream = new MemoryStream())
using (var zipStream = new ZipOutputStream(memoryStream))
{
var crc = new Crc32();
crc.Reset();
crc.Update(fileBytes);
var zipEntry =
new ZipEntry(fileName)
{
Crc = crc.Value,
DateTime = DateTime.Now,
Size = fileBytes.Length
};
zipStream.PutNextEntry(zipEntry);
zipStream.Write(fileBytes, 0, fileBytes.Length);
zipStream.Finish();
zipStream.Close();
return memoryStream.ToArray();
}
}
Usage:
var fileBytes = File.ReadAllBytes(#"C:/1.xml");
var zipBytes = CreateZip(fileBytes, "MyFile.xml");
File.WriteAllBytes(#"C:/2.zip", zipBytes);
This CreateZip method is optimized for the cases when you already have bytes in memory and you just want to compress them and send without even saving to disk.