Solved

Are these 2 codes the same?

Posted on 2013-06-07
4
277 Views
Last Modified: 2013-06-11
I have 2 pieces of code;
1. In the first one, I use RadUpload, read the file, encrypt, save it in database.  I used a 3rd party fax web services and fax the file. This works

2. In the second one, I want to re-fax the docs BUT don't want the user to reattach them. So, I read the file back, decrypt, use MemoryStream (because I don't have the RadUpload anymore) and fax using the 3rd party fax web services. In this, the fax fails because it looks like the doc is corrupted. I debugged: the size of files look the same. I also added code to view the document and it opens just fine and I can see the content.

I just want to make sure these 2 codes are the same. I'm thinking the Memorysteam might be wrong. Sorry if this is too long.

This works

foreach (UploadedFile file in RadUpload1.UploadedFiles)
                    {
                        string documentType = GetFileExtension(file.GetExtension());
                      
                        byte[] bytes = new byte[file.ContentLength];
                        file.InputStream.Read(bytes, 0, file.ContentLength);

                   
                        var key = System.Text.ASCIIEncoding.ASCII.GetBytes(sSecretKey);
                        var encryptedFile = EncryptDecrypt.EncryptData(bytes, key, key);

                         

                            string result = EncryptDecrypt.SaveEncryptedFile(encryptedFile, file.GetName(), documentType,
                                                                            UserSession, rowId, System.Text.Encoding.UTF8.GetBytes(sSecretKey));
                            

                             // keep the original length
                        int orgLength = data.Length;


                        // resize the array: it should be the original length + the new one
                        data = (byte[])ResizeArray(data, orgLength + bytes.Length);


                        // add the new bytes to the array
                        Array.Copy(bytes, 0, data, orgLength, bytes.Length);


                        fileTypes = fileTypes + ";" + file.GetExtension().TrimStart('.') + ";";
                        fileSizes = fileSizes + ";" + bytes.Length + ";";


                        fileTypes = fileTypes.Remove(fileTypes.LastIndexOf(";"));
                        fileSizes = fileSizes.Remove(fileSizes.LastIndexOf(";"));

                        if (fileTypes.Substring(0, 1) == ";")
                            fileTypes = fileTypes.Remove(0, 1);

                        if (fileSizes.Substring(0, 1) == ";")
                            fileSizes = fileSizes.Remove(0, 1);
                        
                        DateTime postponeTime = DateTime.Now.AddDays(-2);
            // in two hours. use any PAST time to send ASAP
            int retriesToPerform = 2;
            string csid = "";
            string pageHeader = "To: " + search_physician_first_name.Text + " " +
                                search_physician_last_name.Text;
            string subject = "Now Document Fax " + pageHeader;
            string replyAddress = faxEmails + ";" + ContactEmail + ";" + txtEmail.Text;  //
            string pageSize = "A4";
            string pageorientation = "Portrait";
            bool isHighResolution = false;
            //this will speed up your transmission
            bool isFineRendering = false;
            //fine will fit more graphics, while normal (false) will fit more textual documents


                         ..... //use a 3rd party fax system 
                         long st2 = ifws.SendfaxEx_2(username, password, faxnumber,
                                               search_physician_first_name.Text + " " +
                                               search_physician_last_name.Text, data, fileTypes, fileSizes,
                                               postponeTime, retriesToPerform, csid,
                                               pageHeader, "", subject, replyAddress, pageSize, pageorientation,
                                               isHighResolution, isFineRendering);
  ....

Open in new window


This one uses MemorySteam and the fax fails

 using (SqlDataReader sdr = objSqlCmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    bytesFile = (Byte[]) sdr[2];
                    fileExt = sdr[1].ToString();
                    //fileName = sdr[0].ToString();

                }
            }

            var decryptFile = bytesFile; 
            var decrypted = EncryptDecrypt.EncryptData(decryptFile, key, key, false);

            string documentType = GetFileExtension(fileExt);

            MemoryStream ms = new MemoryStream(decrypted, false);
            byte[] bytes = new byte[ms.Length];

            // keep the original length
            int orgLength = data.Length;


            // resize the array: it should be the original length + the new one
            data = (byte[])ResizeArray(data, orgLength + bytes.Length);

            // add the new bytes to the array
            Array.Copy(bytes, 0, data, orgLength, bytes.Length);


            fileTypes = fileTypes + ";" + documentType.TrimStart('.') + ";";
            fileSizes = fileSizes + ";" + bytes.Length + ";";


            fileTypes = fileTypes.Remove(fileTypes.LastIndexOf(";"));
            fileSizes = fileSizes.Remove(fileSizes.LastIndexOf(";"));

            if (fileTypes.Substring(0, 1) == ";")
                fileTypes = fileTypes.Remove(0, 1);

            if (fileSizes.Substring(0, 1) == ";")
                fileSizes = fileSizes.Remove(0, 1);
            
           DateTime postponeTime = DateTime.Now.AddDays(-2);
            // in two hours. use any PAST time to send ASAP
            int retriesToPerform = 2;
            string csid = "";
            string pageHeader = "To: " + search_physician_first_name.Text + " " +
                                search_physician_last_name.Text;
            string subject = "Now Document Fax " + pageHeader;
            string replyAddress = faxEmails + ";" + ContactEmail + ";" + txtEmail.Text;  //
            string pageSize = "A4";
            string pageorientation = "Portrait";
            bool isHighResolution = false;
            //this will speed up your transmission
            bool isFineRendering = false;
            //fine will fit more graphics, while normal (false) will fit more textual documents
 
           long st2 = ifws.SendfaxEx_2(username, password, faxnumber,
                                   search_physician_first_name.Text + " " +
                                   search_physician_last_name.Text, data, fileTypes, fileSizes,
                                   postponeTime, retriesToPerform, csid,
                                   pageHeader, "", subject, replyAddress, pageSize, pageorientation,
                                   isHighResolution, isFineRendering);

Open in new window


Common code

 public static Array ResizeArray(System.Array oldArray, int newSize)
    {
        int oldSize = oldArray.Length;
        System.Type elementType = oldArray.GetType().GetElementType();
        System.Array newArray = System.Array.CreateInstance(elementType, newSize);

        int preserveLength = System.Math.Min(oldSize, newSize);

        if (preserveLength > 0)
            System.Array.Copy(oldArray, newArray, preserveLength);

        return newArray;
    } 

Open in new window

0
Comment
Question by:Camillia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39232645
>>I debugged: the size of files look the same
did u compare the bytes in the stream in the 2 cases?
are they identical (besides the size)?
0
 
LVL 7

Author Comment

by:Camillia
ID: 39232972
>> did u compare the bytes in the stream in the 2 cases?

You mean compare what's in "data" field? "data" is the final output. if so, no. Let me check that.
0
 
LVL 7

Author Comment

by:Camillia
ID: 39233409
I put a debug step and captured "data" field. I used this site to compare
http://www.diffnow.com/

and the bytes were the same. I copied/pasted from the watch window and the values were like this
[0]      37      byte
[1]      80      byte
...

I also added this code to the second code above and I can open the file. I've emailed the 3rd party fax company. If I can't figure it out, I'll just have the user reattach the docs. I don't want to do that but I guess i have no other options

 //Response.Buffer = true;
            //Response.Charset = "";
            //Response.ClearHeaders();
            //Response.AddHeader("Cache-Control", " no-store, no-cache ");

            //Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
            //Response.AddHeader("content-length", decrypted.Length.ToString());
            //Response.ContentType = fileExt;
            //Response.BinaryWrite(decrypted);

            //Response.Flush();
            //Response.End();

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 39236541
You're right, the bytes aren't correct. Not sure why this comes out as all zeros

 MemoryStream ms = new MemoryStream(decrypted, false);  
            byte[] bytes = new byte[ms.Length];  //*** bytes comes out as all zeros
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question