Solved

Are these 2 codes the same?

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 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