Praveen Venugopal

Praveen Venugopal

  • NA
  • 127
  • 75.9k

How to read uploaded files from a datagrid

Aug 14 2008 1:33 AM

 There!,

I have uploaded some text documents from my web page(C#) to my application .

i am populating those uploaded files name in a datagrid .

now if click on a document's name it should be opened for reading.

Currently my code is,


HttpFileCollection files = Request.Files;

for (int p = 0; p < files.Count; p++)
{

HttpPostedFile file = files[p];if (file.ContentLength == 0)
{

continue;
}

string c = System.IO.Path.GetFileName(String.Format(file.FileName));

string path = Server.MapPath("../")+"Upload"+ @"\" +c;
 

StreamWriter streamWriter;streamWriter = File.CreateText(path);
streamWriter.Close();

file.SaveAs(path);


protected void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (!(e.Item.Cells[0].Text == "Documents"))
        {
            if (((e.Item.ItemType == ListItemType.Item)
                        || (e.Item.ItemType == ListItemType.AlternatingItem)))
            {

                e.Item.Cells[0].Attributes.Add("style", "cursor:hand");
                HyperLink lnk = new HyperLink();
                string str = e.Item.Cells[0].Text.ToString();
                string path = Server.MapPath("../Upload") + @"\" + e.Item.Cells[0].Text.ToString();
                lnk.NavigateUrl = "http://localhost/" + path;
                e.Item.Cells[0].Controls.Add(lnk);
                lnk.Text = str;

             
            }
        }
    }


but it is not working .Could you please tell me a solution?


Answers (1)