Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Passing bytes array back and forth
Message
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Passing bytes array back and forth
Versions des environnements
Environment:
C# 5.0
OS:
Windows 10
Database:
MS SQL Server
Divers
Thread ID:
01648484
Message ID:
01648484
Vues:
37
Hi everybody,

I am wondering if you can see if something I am doing may be wrong. I'm using file uploader, I send the information back to the web page, then back again and finally reading it and converting that byte array into hex. I am wondering if there may be an issue here and what should I use to correct it.

Here is the code that is executed to read file's content first time and send it back to the page (the DataObject defined as an Object in the class):
var resultOut = new List<ImageUploadResult>();

                    var streamProvider = new MultipartMemoryStreamProvider();
                    streamProvider = await Request.Content.ReadAsMultipartAsync(streamProvider);

                    foreach (
                        var item in
                        streamProvider.Contents.Where(c => !string.IsNullOrEmpty(c.Headers.ContentDisposition.FileName))
                    )
                    {
                        using (Stream stFileSource = new MemoryStream(await item.ReadAsByteArrayAsync()))                     {
                            byte[] fileBytes;
                            
                            fileBytes = new Byte[stFileSource.Length];
                            stFileSource.Read(fileBytes, 0, Convert.ToInt32(stFileSource.Length));

                            resultOut.Add(new ImageUploadResult()
                            {
                                FileName = item.Headers.ContentDisposition.FileName,
                                DataObject = fileBytes,
                                Key = "" // Guid.NewGuid().ToString()
                            });
                        }
                    }
                    return Request.CreateResponse(HttpStatusCode.OK, resultOut.ToArray());
and this is the code which is saving that info after it was send back again:
 StringBuilder sb = new StringBuilder();
                    foreach (ImageUploadResult attachment in messageViewModel.Attachments)
                    {
                        byte[] fileArray = Encoding.UTF8.GetBytes(attachment.DataObject.ToString());
                        // attachment.FileName already has double quotes, for some reason
                        sb.Append("<attachment filename=" + attachment.FileName + "><data format=\"hex\">" + 
                            fileArray.ToHexString() + "</data></attachment>");
                    }
So, my concern here about getting that byte array for the file correctly.

We got it the first time, send to the page (it got somehow serialized into JSON) and then received back. The ImageUploadResult uses object for the DataObject.

So, I am wondering if that fileArray in the second method is correct. I originally tried ASCII but I was unable to open that attachment. So, I am wondering if Encoding.UTF8 is what I should try or what should I try?

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Répondre
Fil
Voir

Click here to load this message in the networking platform