---
title: "How to clone a HttpPostedFile"  
description: "How to clone a HttpPostedFile"  
author: "Anonymous User"  
published: 2015-06-02  
updated: 2015-06-02  
canonical: https://www.mindstick.com/forum/23277/how-to-clone-a-httppostedfile  
category: "asp.net"  
tags: ["c#", "http post"]  
reading_time: 2 minutes  

---

# How to clone a HttpPostedFile

I have an [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) that allows a user to [upload](https://www.mindstick.com/forum/12860/how-to-upload-file-asynchronously-using-generic-handler) a file, the file is scanned with Symantec [protection](https://www.mindstick.com/blog/301143/how-to-wear-knee-braces-for-the-best-protection) engine before it is saved. The [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) 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](https://www.mindstick.com/forum/145447/what-are-objects), but my [uploaded files](https://www.mindstick.com/forum/34650/how-can-download-uploaded-files-in-mvc) are not all Serializable. I've also tried resetting the [stream](https://yourviews.mindstick.com/view/88509/us-intercepts-iranian-tanker-m-t-stream-amid-hormuz-tensions) 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](https://www.mindstick.com/blog/12298/use-custom-writing-services-to-get-through-the-finals) [connection](https://www.mindstick.com/articles/13012/what-makes-ethernet-connection-better-than-wifi) 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](https://www.mindstick.com/articles/23207/how-to-find-the-best-fit-job-for-anyone) needs that, but it is quite large.

## Replies

### Reply by 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 engineStreamScanRequest 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;
```


---

Original Source: https://www.mindstick.com/forum/23277/how-to-clone-a-httppostedfile

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
