ComponentOne Upload for ASP.NET WebForms
Task-Based Help / Implementing an Anti-Virus File Scan in C1Upload
In This Topic
    Implementing an Anti-Virus File Scan in C1Upload
    In This Topic

    In this example, you will set valid file extensions for files that can be uploaded and scan the files to check to see if they are infected. The upload will be cancelled if a file is infected.

    1. Add a Label control to your Web project.
    2. Add a C1Upload control to your Web project.
      The C1Upload control gets registered in web.config file. If your project is hosted on a server, you may need to register your control manually in the <system.web> node of your web.config file, using the steps mentioned within third step of Create the Project and add C1Upload topic.
    3. In the Visual Studio Properties window, next to the TargetFolder property define the virtual path for Upload directory.
    4. Next to the ValidFileExtensions property, enter .doc,.jpg. This will filter the type of files that can be uploaded.        
    5. We will subscribe the ValidatingFile event of C1Upload and scan the file being uploaded. For this, we will run an instance of the AVG application and check whether the file is infected or not. Note that for this example we are using AVG 10 (free version - http://www.freeavg.com/?lng=in-en&cmpid=free).

      The results of the virus scan are written in a Report.txt which is saved in the Temporary storage of C1Upload.

      Next, we will use a StreamReader object to read the Report.txt file and check whether it contains a "Found infections" string. If the string is found, then we will cancel the upload process by calling e.IsValid = False and we will then delete the file being uploaded using File.Delete().

    6. Select View | Code and add the following code to the Default.aspx.cs:

             

          protected void u1_ValidatingFile(object sender,
         C1.Web.Wijmo.Controls.C1Upload.ValidateFileEventArgs e) 
              { 
                 var file = e.UploadedFile; 
          // remove foreach 
          // foreach (C1FileInfo file in C1Upload1.UploadedFiles) 
             try
          { 
          //validate file 
          // Note: In this arg, "TempFileName" and "FileName" is a read-only property. 
          // you can pass a message in “e.Message” 
          } 
          catch (Exception ex) 
          { 
          //handle exception 
          } 
          }
      
    See Also