Solved

Are these 2 codes the same?

Posted on 2013-06-07
4
267 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
Comment Utility
>>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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.NET Error 7 42
Editing XSL files, 2 18
Expando 4 33
How to fill empty cells of one Microsoft Access database using C# 5 38
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now