如何克隆HttpPostedFile

rog*_*uce 2 c# asp.net

我有一个允许用户上载文件的应用程序,在保存文件之前,已使用Symantec保护引擎对其进行了扫描。我遇到的问题是,使用保护引擎扫描文件后,它们的字节数为0。我正在尝试解决这个问题。

我尝试了这里提到的克隆解决方案: 深度克隆对象,但是我上传的文件并非全部可序列化。我还尝试过在扫描引擎类中将流重置为0,然后再将其传递回保存。

我已经与Symantec联系,他们说为此应用程序编写的自定义连接类看起来正确,并且保护引擎没有抛出错误。

我愿意解决这个问题。

这是文件上传的代码:

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(); }
Run Code Online (Sandbox Code Playgroud)

如果有人需要,我可以提供连接类代码,但是它很大。

Reb*_*cca 5

您可以在将文件流传递到扫描引擎之前对其进行备份。

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);
//...
Run Code Online (Sandbox Code Playgroud)

更新 要复制流,您可以执行以下操作:

MemoryStream ms = new MemoryStream();
file.InputStream.CopyTo(ms);
file.InputStream.Position = ms.Position = 0;
Run Code Online (Sandbox Code Playgroud)