Reputation: 1529
What i am trying to do is, i am getting file name/path using asp.net uploader control and then saving its path it grid view. e.g
String path = String.Empty;
path = FileUploader.FileName;
and then saving this path in grid view column.
savefiletoGrid(path);
After uploading all required files i am saving these file on server. like this
while( // condition )
{
string tempfilename = ""; // file name/path from gridview
string path2 = Server.MapPath("Dir\\" + tempfilename);
FileUploader.SaveAs(path2);
}
But, problem is that file is being saved on server with correct name but with size 0 byte. Please let me know how to solve this issue ?
Actually i want something like client upload in asp.net, i 'll upload more than one file and show them in gridview ( or in something else ) so that user can see files to be selected and can delete from listed files.
File 'll be saved to server only when user click some other button say 'Update'. could you please help me , how to accomplish this ?
Upvotes: 0
Views: 10854
Reputation: 1529
Problem has been solved by creating a DataTable with one column for control (with column data Type FileUpload) for example :
private DataTable CreateDtDocs(string name, string path, FileUpload FileUploader)
{
DataTable dt1 = new DataTable();
dt1.Columns.Add("SR_NO");
dt1.Columns.Add("Name");
dt1.Columns.Add("Path");
Type col_type = fubrowse.GetType();
DataColumn dt_col = new DataColumn("Control", col_type);
dt1.Columns.Add(dt_col);
DataRow dr = dt1.NewRow();
dr["SR_NO"] = "1";
dr["NAME"] = name;
dr["Path"] = path;
dr["Control"] = FileUploader;
dt1.Rows.Add(dr);
return dt1;
}
And then Populate table like below :
private DataTable AddDtDocs(string name, string path, FileUpload FileUploader)
{
DataTable dt1 = (DataTable)Session["AttachFilesdt"];
int count = dt1.Rows.Count;
DataRow dr = dt1.NewRow();
dr["SR_NO"] = count + 1;
dr["NAME"] = name;
dr["Path"] = path;
dr["Control"] = FileUploader;
dt1.Rows.Add(dr);
return dt1;
}
And then i am adding path name and control in Dictionary and passing them to a different function to save them on server.
Dictionary<string, FileUpload> DocsPathAndControl = new Dictionary<string, FileUpload>();
if (Session["AttachFilesdt"] != null)
{
tempdt = (DataTable)Session["AttachFilesdt"];
for (int i = 0; i < tempdt.Rows.Count; i++)
{
DocsPathAndControl.Add(tempdt.Rows[i]["Path"].ToString(), (FileUpload)tempdt.Rows[i]["Control"]);
}
Session["AttachFilesdt"] = null;
}
Function to save Files
private void AddDocuments(int jurisdictionID, Dictionary<string,FileUpload> docPathsAndControl)
{
foreach (var item in docPathsAndControl)
{
string tempfilename = jurisdictionID + "_" + item.Key.ToString();
string path = Server.MapPath("Dir\\" + tempfilename);
FileUpload FileUploaderControl = (FileUpload)item.Value;
FileUploaderControl.PostedFile.SaveAs(path);
}
}
Hope, it 'll help.
Upvotes: 0
Reputation: 2477
To accomplish this functionality, you'll have to let the user upload the files. You must save them temporarily to display them. Then, upon the user clicking an 'Update' button, you will transfer the temporary files to your permanent storage.
Do you keep the FileUploaders in a GridView?
Upvotes: 2
Reputation: 307
You have to catch the event generated in GridView in its RowCommand event also set a CommandName property for upload button.
Following is the detailed code through which you can accomplish this:
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" CommandName="Upload"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And in you code behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Upload")
{
FileUpload FileUp = (FileUpload)e.Item.FindControl("FileUpload1");
string UploadedFileName = FileUp.FileName;
string Path = Server.MapPath("Documents");
FileUpload.SaveAs(Path + "\\" + UploadedFileName);
}
}
Hope it helps.
Upvotes: 3