首页 文章

如何克隆HttpPostedFile

提问于
浏览
1

我有一个允许用户上传文件的应用程序,该文件在保存之前使用Symantec保护引擎进行扫描 . 我遇到的问题是,在使用保护引擎扫描文件后,它们有0个字节 . 我正试图找到解决这个问题的方法 .

我已经尝试过这里提到的克隆解决方案:Deep cloning objects,但我上传的文件并非都是Serializable . 我还尝试将流重置为扫描引擎类中的0,然后再将其传回保存 .

我一直与赛门铁克联系,他们说为这个应用程序编写的自定义连接类看起来是正确的,保护引擎不会抛出错误 .

我愿意解决这个问题 .

以下是上传文件的代码:

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(); }

如果有人需要,我可以提供连接类代码,但它非常大 .

1 回答

  • 4

    您可以在将文件流传递到扫描引擎之前获取文件流的副本 .

    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 要复制流,您可以执行以下操作:

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

相关问题