Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Are these 2 codes the same?

Posted on 2013-06-07
4
Medium Priority
?
278 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

705 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