How to clone a HttpPostedFile

I have an application that allows a user to upload a file, the file is scanned with Symantec protection engine before it is saved. The problem I'm encountering is that after the files are scanned with protection engine they have 0 bytes. I'm trying to come up with a solution to this problem. 

I've tried the cloning solution mentioned here: Deep cloning objects, but my uploaded files are not all Serializable. I've also tried resetting the stream to 0 in the scan engine class before passing it back to be saved. 

I have been in contact with Symantec and they said the custom connection class that was written for this application looks correct, and protection engine is not throwing errors. 

I'm open to any solution to this problem. 

Here is the code where the file is uploaded: 

private void UploadFiles()
            {                    System.Web.HttpPostedFile objFile;                    string strFilename = string.Empty;
                        objFile = FileUpload1.PostedFile;                        strFilename = FileUpload1.FileName;  
                        if (GetUploadedFilesCount() < 8)                        {
     if(IsDuplicateFileName(Path.GetFileName(objFile.FileName)) == false)                            {
    if(ValidateUploadedFiles(FileUpload1.PostedFile) == true)
           //stores full path of folder
                string strFileLocation = CreateFolder();
             //Just to know the uploading folder
                                    mTransactionInfo.FileLocation = strFileLocation.Split('\\').Last();
    if(ScanUploadedFile(objFile) == true)
           {                                            SaveFile(objFile, strFileLocation);
                {                                       lblErrorMessage.Visible = true;
          if(mFileStatus != null)
              { lblErrorMessage.Text = mFileStatus.ToString(); }

I can provide the connection class code if anyone needs that, but it is quite large.

Last updated:6/2/2015 8:02:59 AM

1 Answers

Anonymous User
Anonymous User

You could take a copy of the filestream before you pass it into the scanning engine. 

byte[] fileData = null;
using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
    fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
// pass the scanning engine
StreamScanRequest scan = requestManagerObj.CreateStreamScanRequest(Policy.DEFAULT);

Update To copy the stream you can do this: 

MemoryStream ms = new MemoryStream();
file.InputStream.Position = ms.Position = 0;