Solved

Are these 2 codes the same?

Posted on 2013-06-07
4
276 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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