MailClient With Delete Uploaded Files on Session_End

Thread is closed for posting
5 posts, 0 answers
  1. 63F75A2C-1F16-4AED-AFE8-B1BBD57646AD
    63F75A2C-1F16-4AED-AFE8-B1BBD57646AD avatar
    1572 posts
    Member since:
    Oct 2004

    Posted 06 Mar 2007 Link to this post

    Requirements

    RadControls version

    Telerik.Web.UI 2007.3.12.18+
    or
    RadUpload v.2.0.0+

    .NET version

    NET 2.x/3.x

    Visual Studio version

    2005/2008

    programming language

    C#

    browser support

    all browsers supported by RadControls


     
  2. PROJECT DESCRIPTION
    The project demonstrates how to use RadUpload for uploading files which will be mail attachments on sending an e-mail. Information about each uploaded file is stored in the session and should the session expire, Session_End in Global.asax is fired where the uploaded files are being deleted:
    void Session_End(object sender, EventArgs e)     
    {     
        Hashtable data = (Hashtable)Session["TempUplFiles"];     
        
        foreach (DictionaryEntry entry in data)     
        {     
            ArrayList uploadedFilesInfo = (ArrayList)entry.Value;     
            for (int i = 0; i < uploadedFilesInfo.Count; i++)     
            {     
                UploadedFileInfo fileInfo = (UploadedFileInfo)uploadedFilesInfo[i];     
        
                if (System.IO.File.Exists(fileInfo.TempFilePath))     
                {     
                    System.IO.File.Delete(fileInfo.TempFilePath);     
                }     
            }     
        }     
    }   

    The session stores information within a hash table where keys are unique identifiers of each session started and values are ArrayLists with information about the uploaded files during the corresponding session.

    For versions Q1 2010 +, please use the updated archive.

  • 2151CBA9-EA16-4A5A-B193-6B06453A186A
    2151CBA9-EA16-4A5A-B193-6B06453A186A avatar
    5 posts
    Member since:
    Dec 2009

    Posted 25 Jan 2012 Link to this post

    I'm running your latest example (MailClientWebUIUpdated.zip) using Q2 2009 SP1.
    When I look at the received emails the attachments have the TempFilePath as the file name instead of the uploaded name (OriginalFileName). i.e. I attach file 'Photo of House.jpg', the recipient gets an email with a file '2eb90420-2af9-4500-8f04-66a077c341f9.dat' attached. Is there a step missing? 

    I also note that the code block for the current postback never runs, only that for the previously uploaded files.

    //Attach the files, uploaded with the current postback
           foreach (UploadedFile currentUploadedFile in RadUpload1.UploadedFiles)
           {
               CustomUploadedFileInfo fileInfo = new CustomUploadedFileInfo(currentUploadedFile.GetName());
               SessionUplFiles.Add(fileInfo);
               currentUploadedFile.SaveAs(fileInfo.TempFilePath);
               message.Attachments.Add(new Attachment(currentUploadedFile.InputStream, currentUploadedFile.GetName()));
     
           }
     
           //Attach the previously uploaded files
           foreach (CustomUploadedFileInfo info in SessionUplFiles)
           {
               message.Attachments.Add(new Attachment(info.TempFilePath));
           }
     
           message.Body = txtBody.Text;

    Many thanks in advance.
    Keith
  • 0E2F26BC-A45A-41DA-A470-D57CB32DFE93
    0E2F26BC-A45A-41DA-A470-D57CB32DFE93 avatar
    1028 posts
    Member since:
    Mar 2017

    Posted 08 Feb 2012 Link to this post

    Hi Keith,

    RadUpload control uses standard input type='file' control. When the attach button is clicked a postback occurs and the file is uploaded. Information about the uploaded files is saved in the SessionUplFiles variable. If you click send without attaching the file the following will be executed:
    //Attach the files, uploaded with the current postback
           foreach (UploadedFile currentUploadedFile in RadUpload1.UploadedFiles)
           {
               CustomUploadedFileInfo fileInfo = new CustomUploadedFileInfo(currentUploadedFile.GetName());
               SessionUplFiles.Add(fileInfo);
               currentUploadedFile.SaveAs(fileInfo.TempFilePath);
               message.Attachments.Add(new Attachment(currentUploadedFile.InputStream, currentUploadedFile.GetName()));
      
           }

    Otherwise, the SessionUplFiles variable will be iterated.

    The issue with your case is because the uploaded file is saved with a different name than the original (GUID). And the Attachment is sending the file with the temp file name.

    Here is an example how to retrieve the file and set the original name :
    //Attach the previously uploaded files
    foreach (CustomUploadedFileInfo info in SessionUplFiles)
    {
        //message.Attachments.Add(new Attachment(info.TempFilePath));
        var test = new FileStream(info.TempFilePath, FileMode.Open, FileAccess.Read);
        message.Attachments.Add(new Attachment(test, info.OriginalFileName));
        test.Close();
    }
    For more information please review the following help article.

    Regards,
    Peter Filipov
    the telerik team
  • 2151CBA9-EA16-4A5A-B193-6B06453A186A
    2151CBA9-EA16-4A5A-B193-6B06453A186A avatar
    5 posts
    Member since:
    Dec 2009

    Posted 10 Feb 2012 Link to this post

    Hi Peter,
    Many thanks for the reply. Partial success - when I get to actually send the message (with an attachment), I get an InnerException {"Cannot access a closed file."} It works if I comment out test.Close(). So as long as I don't run into any more problems I'll leave it commented out. I guess i'm worrying about memory leaks?
    Btw, I am converting all the code into VB.net, could this have some bearing?
    Thanks again,
    Keith
  • 0E2F26BC-A45A-41DA-A470-D57CB32DFE93
    0E2F26BC-A45A-41DA-A470-D57CB32DFE93 avatar
    1028 posts
    Member since:
    Mar 2017

    Posted 14 Feb 2012 Link to this post

    Hi Keith,

    I tested the C# example and everything works fine at my side (no exceptions are thrown). If you want you can open a support ticket and send me your VB version to investigate it locally.

    Kind regards,
    Peter Filipov
    the Telerik team

    Consider using RadControls for ASP.NET AJAX (built on top of the ASP.NET AJAX framework) as a replacement for the Telerik ASP.NET Classic controls, See the product support lifecycle here.

  • Back to Top

    This Code Library is part of the product documentation and subject to the respective product license agreement.