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:
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();
}