Home > DeveloperSection > Forums > How to clone a HttpPostedFile
Ankit Singh

Total Post:341

Points:2389
Posted on    June-02-2015 7:36 AM

 ASP.Net C#  Http Post 
Ratings:


 1 Reply(s)
 641  View(s)
Rate this:

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.



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    June-02-2015 8:02 AM

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;


Don't want to miss updates? Please click the below button!

Follow MindStick