|
public void CreatTable ()
{
upfiles = new DataTable ("upfile");
DataColumn [] keys = new DataColumn [1];
keys [0] = upfiles.Columns.Add ("filename", typeof (string));
keys [0] .AllowDBNull = false;
upfiles.PrimaryKey = keys;
upfiles.Columns.Add ("filestream", typeof (Stream));
fname.Text = "";
}
/// <summary>
/// add attachment to list
/// </ summary>
private void AddFile ()
{
if (upfiles == null)
{
CreatTable ();
}
if (myFile.PostedFile.FileName! = "")
{
string nam = myFile.PostedFile.FileName;
int i = nam.LastIndexOf ("\\");
string filename = nam.Substring (i + 1);
if (upfiles.Rows.Find (filename) == null)
{
Stream fstream = myFile.PostedFile.InputStream;
DataRow dr = upfiles.NewRow ();
dr [0] = filename; // Get the file name
upfiles.Rows.Add (dr); // increase
ArrayList dc = new ArrayList ();
for (int j = 0; j <upfiles.Rows.Count; j ++)
{
DataRow df = upfiles.Rows [j];
dc.Add (df [0] .ToString ());
}
files.DataSource = dc;
files.DataBind ();
}
}
}
/// <summary>
/// remove attachment from list
/// </ summary>
private void RemoveFile ()
{
if (files.SelectedItem.Value! = null)
{
string fname = files.SelectedItem.Value;
DataRow foundRow = upfiles.Rows.Find (fname);
if (foundRow! = null)
upfiles.Rows.Remove (foundRow);
ArrayList dc = new ArrayList ();
for (int j = 0; j <upfiles.Rows.Count; j ++)
{
DataRow df = upfiles.Rows [j];
dc.Add (df [0] .ToString ());
}
files.DataSource = dc;
files.DataBind ();
}
}
/// <summary>
/// send email
/// </ summary>
private void UploadFile ()
{
string userId = this.GetSessionState ("userId");
if (upfiles.Rows.Count> 0)
{
fname.Text = "";
for (int j = 0; j <upfiles.Rows.Count; j ++)
{
DataRow dr = upfiles.Rows [j];
string topath = Server.MapPath ((this.GetSessionState ("userId") + "/") + dr [0]); // Set attachment path
fname.Text + = topath.ToString () + ";";
myFile.PostedFile.SaveAs (topath); // Save attachment
}
When I insert "multi-attachment upload address" fname.Text into the database, the following happens:
For example: When I upload 3 files at a time, 3 rows of records are generated in the database:
A: / aaa;
A: / aaa; B: / bbb;
A: / aaa; B: / bbb; C: / ccc;
And what I want is to generate only one row of records in the database for each upload, such as:
A: / aaa; B: / bbb; C: / ccc;
Please help me. |
|