Click here to Skip to main content
15,881,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code behind file where button click event is there and i am saving all the attachments that are sent in a folder but now i want to create a folder with email subject and save that attachment in that folder pls help!!!
C#
protected void Button1_Click(object sender, EventArgs e)
{ 
    string temp = "";
    Email em = new Email();
   
    if (FileUpload1.HasFile)
    {      
        System.IO.Directory.CreateDirectory(MapPath("~/Mail_Attachments/" + txtSubject.Text ));
       
        FileUpload1.SaveAs(MapPath("~/Mail_Attachments/" + txtSubject.Text + FileUpload1.FileName));

        temp = "~/Mail_Attachments/" + txtSubject.Text + FileUpload1.FileName;
    }

    foreach (GridViewRow row in GridView1.Rows)
    {
        Label Name = row.FindControl("Label1") as Label;
        Label Email_Id = row.FindControl("Label2") as Label;
        Label Date_Time = row.FindControl("Label4") as Label;
        CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
        
        if (cb.Checked == true)
        {
            Dashboard dsb = new Dashboard();
            dsb.Name = Name.Text;
            dsb.Email_Id = Email_Id.Text;
            dsb.Date_Time = System.DateTime.Now.ToString();
            dsb.Subject = txtSubject.Text;
            dsb.Body = txtBody.Text;
            dsb.Attachments = temp;
            md.Dashboards.InsertOnSubmit(dsb);
            md.SubmitChanges();
            //em.InsertData(Name.Text, Email_Id.Text, System.DateTime.Now.ToString(),txtSubject.Text,txtBody.Text,temp);
        }
    }
}
Posted
v2
Comments
[no name] 7-Jan-15 3:40am    
quest not clear..do u wanted to create each folder on every data submission?

Try this. Hope this what you want.

C#
if (FileUpload1.HasFile)
    {

        string directoryPath = "~/Mail_Attachments/" + txtSubject.Text; 
        string filePath =  directoryPath + "/" +  FileUpload1.FileName;
        if (!Directory.Exists(MapPath(directoryPath)))
              System.IO.Directory.CreateDirectory(MapPath(directoryPath ));
 
        FileUpload1.SaveAs(MapPathfilePath  ));

        temp = filePath ;
    }
 
Share this answer
 
v3
Comments
Parth Mashroo 7-Jan-15 4:54am    
it is showing error physical path, but a virtual path was expected. at line where system.io is written and in the line mappathfilepath is not written in correct format pls help on that and thanx for reply!!
ArunRajendra 7-Jan-15 5:09am    
Check the modified solution.
Parth Mashroo 7-Jan-15 5:39am    
sir there is no change in the code!!
ArunRajendra 7-Jan-15 6:16am    
I have added MapPath to Directory.Exists.
Parth Mashroo 7-Jan-15 7:43am    
sir i have made slight changes to your code and it worked but now there is another problem i.e i had to convert the directory path to virtual path so i had to convert it so now the problem is shown in this question and also the changed code is also uploaded :: http://www.codeproject.com/Questions/861948/How-to-convert-physical-path-to-virtual-path-dynam

thanx to ur help i got this far but pls help me on this one and once again thanx!!!
Can u try this code..

HTML
string directoryPath = Server.MapPath(string.Format("~/Mail_Attachments/"+ txtSubject.Text));
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);

FileUpload1.SaveAs(MapPath("~/Mail_Attachments/" + txtSubject.Text + FileUpload1.FileName));

                }
 
Share this answer
 
Comments
Parth Mashroo 7-Jan-15 4:56am    
yes sir i have tried that code but after that code a folder is getting created but i want to insert the file in that folder after getting created on same button click event pls help on that and thanx for reply!!!
[no name] 7-Jan-15 5:18am    
string str = Directory.CreateDirectory(directoryPath).ToString();

remove the "Mail_attachment" and put str into save as position.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900