Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone a HttpPostedFile

Tags:

c#

asp.net

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;
    if (FileUpload1.HasFile)
    {

        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);
                    }
                    else
                    {
                        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.

like image 712
rogerdeuce Avatar asked Feb 12 '23 02:02

rogerdeuce


1 Answers

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.CopyTo(ms);
file.InputStream.Position = ms.Position = 0;
like image 188
Rebecca Avatar answered Feb 13 '23 20:02

Rebecca