Click here to Skip to main content
15,888,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am wondering if the following is possible as it is out of my coding skills: I am coding in Visual Studio 2010 using asp.net webforms and C#

I have a database with a table called 'Incident' that will store details relating to an incident such as Incident_ID, User_ID, Business_ID, Incident_Type_ID, Description, DateTime, File_Path.

I want to be able to upload files such as video and images to a folder on my web hosting server and link that to the logged in user and business in some way(I.e by creating a folder that has the business name, then a sub folder with the username of the user and in this folder store the incident files. Then when a user from that business logs in all the files relating to that business named folder will display)

At the same time I want to be able to write data to the database. Then on the PageLoad() I want to be able to view/download/delete the given file and view the associated details from the database with that file such as it's upload date and time, what business uploaded it and what user uploaded it.

I have some code written but it is not working fully. Here is what I have:
C#
protected void UploadFile(object sender, EventArgs e)
        {
            if (btnBrowse.PostedFile != null && btnBrowse.PostedFile.ContentLength > 0)
            {

                string fileName = Path.GetFileName(btnBrowse.PostedFile.FileName);
                string folder = Server.MapPath("~/BusinessProfilesContent/" + Session["BusinessName"]);
                Directory.CreateDirectory(folder);
                btnBrowse.PostedFile.SaveAs(Path.Combine(folder, fileName));
                try
                {
                    
                    Response.Write("Uploaded: " + fileName);
                }
                catch
                {
                    Label1.Text = "Operation Failed!!!";
                }
            }


           
        }

 protected void DownloadFile(object sender, EventArgs e)
        {
            string filePath = (sender as LinkButton).CommandArgument;
            Response.ContentType = ContentType;
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
            Response.WriteFile(filePath);
            Response.End();
        }

        protected void DeleteFile(object sender, EventArgs e)
        {
            string filePath = (sender as LinkButton).CommandArgument;
            File.Delete(filePath);
            Response.Redirect(Request.Url.AbsoluteUri);
        }

 protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "Welcome " + Session["User"];
            Session["BusinessName"] = lblBusiness.Text;
            
            if (!IsPostBack)
            {
                string[] filePaths = Directory.GetFiles(Server.MapPath("~/BusinessProfilesContent/" + Session["BusinessName"]));
                List<listitem> files = new List<listitem>();
                foreach (string filePath in filePaths)
                {
                    files.Add(new ListItem(Path.GetFileName(filePath), filePath));
                }
                GridView1.DataSource = files;
                GridView1.DataBind();
            }
Posted
Updated 19-Feb-14 18:49pm
v3
Comments
The14thNoah 19-Feb-14 19:01pm    
hm. . . . .have you started coding what your project required here? or you just dont have any idea how to start this?
Member 10609511 19-Feb-14 19:15pm    
I have some code written but it is not working fully. Here is what I have:

protected void UploadFile(object sender, EventArgs e)
{
if (btnBrowse.PostedFile != null && btnBrowse.PostedFile.ContentLength > 0)
{

string fileName = Path.GetFileName(btnBrowse.PostedFile.FileName);
string folder = Server.MapPath("~/BusinessProfilesContent/" + Session["BusinessName"]);
Directory.CreateDirectory(folder);
btnBrowse.PostedFile.SaveAs(Path.Combine(folder, fileName));
try
{

Response.Write("Uploaded: " + fileName);
}
catch
{
Label1.Text = "Operation Failed!!!";
}
}



}

protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}

protected void DeleteFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
File.Delete(filePath);
Response.Redirect(Request.Url.AbsoluteUri);
}

protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Welcome " + Session["User"];
Session["BusinessName"] = lblBusiness.Text;

if (!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/BusinessProfilesContent/" + Session["BusinessName"]));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
The14thNoah 19-Feb-14 19:27pm    
Please update /edit your question then put this code for us to read this easily :)
Member 10609511 19-Feb-14 19:28pm    
Does that help?

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