Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Path to file in Virtual folder

Vote:
 

I have a page where I can send a mail to specific users and has added a FileSystemDataSource to list files so I can attach them to the mail.

But to add them to the mail I need the relative path.

How do I get the relative path to an EPiServer Virtual Directory (ie Documents)? 

#29880
May 20, 2009 8:22
Vote:
 

Ok, I found out how:

EPiServer.Web.Hosting.UnifiedFile uf = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetFile(attachment) as EPiServer.Web.Hosting.UnifiedFile;

But the name of the attachmnet isn't the display name, is there anyway to change the name of the attachment in the mail.
 
#29881
May 20, 2009 8:46
Vote:
 

Hi

 I'm not sure if this is what you are asking, but can't you do something like this:

UnifiedFile file = HostingEnvironment.VirtualPathProvider.GetFile(filePath) as UnifiedFile;

if (file != null)

{

using(Stream stream = file.Open())

{

string fileName = "My_file_name";

Attachment attachment = new Attachment(stream, fileName, "application/pdf"); //Or other Mime-type

//Send mail

}

}

#29888
May 20, 2009 10:34
Vote:
 

Thanx for the tip it made me find the .Name attribute on the attachment and instead of using a strem a solved it like this:

//On page:
// -------- START Building attachment string
string attachmentFiles = "";
foreach (ListItem li in CheckBoxListFiles.Items)
{
 if (li.Selected == true)
 {
  attachmentFiles += li.Value + ";";
 }
}
if (attachmentFiles.Contains(";"))
{
 attachmentFiles = attachmentFiles.Substring(0, attachmentFiles.Length - 1);
}

string virtualFolder = FileDataSource.Root;
string attachments = "";
if (attachmentFiles.Length > 0)
{
 foreach (string file in attachmentFiles.Split(';'))
 {
  string virtualFile = virtualFolder + file;
  EPiServer.Web.Hosting.UnifiedFile uf = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetFile(virtualFile) as EPiServer.Web.Hosting.UnifiedFile;
  attachments += uf.LocalPath + "," + file + ";";
 }
 if (attachments.Contains(";"))
 {
  attachments = attachments.Substring(0, attachments.Length - 1);
 }
}
// -------- END Finished Building attachment string
// Pass the attachments string to send mail function


//In the send mail function
if (attachments.Length != 0)
{
 foreach (string attach in attachments.Split(';'))
 {
  string[] file = attach.Split(',');
  Attachment attachment = new Attachment(file[0]);
  attachment.Name = file[1];
  msg.Attachments.Add(attachment);
 }
}

#29893
Edited, May 20, 2009 14:14
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.