What we are going to achieve is to upload a file into a folder inside your web project.
Add a FileUpload control and a button into your web page.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" />
Lets say we want to upload files into a folder named "BookCovers". Add the below code in button clck event.
if (FileUpload1.PostedFile != null)
{
if (FileUpload1.PostedFile.ContentLength > 0)
{
FileUpload1.PostedFile.SaveAs(Server.MapPath(@"~/BookCovers/") + FileUpload1.PostedFile.FileName);
}
}
Now you can save the file path in your database and retrieve the image. You can save the relative path in the database instead of saving the full path. See the post to understand how to get the relative file path.
you can use the below code to get the full path according to this post.
string FullPath = Server.MapPath(@"~/BookCovers/") + FileUpload1.PostedFile.FileName;
you can use the below code to get the full path according to this post.
string FullPath = Server.MapPath(@"~/BookCovers/") + FileUpload1.PostedFile.FileName;
No comments:
Write comments