Hi All,
Currently, I have a file upload control in my aspx page to allow user to upload images.
When the user clicks the SAVE button, the image will be saved in a particular folder named 'PMap' that located within the website.
What I am trying to do here is to direct all uploaded images into a common image folder (in this case 'PMap') and then, retrieve the images and displayed it through a Gridview according to its respective image id. I have managed to store all uploaded images into a common image folder, PMap.
However, I hope advice can be given on my following problems...
My problems are:
1) How do I save the physical path of the image in that particular folder into the database under a column named ImgPath, so that I can 'call' and retrieve the image from the folder using ImgPath?
2) How do I ensure that when I retrieve the image from the folder, it's the image of that particular Image ID?
This is my current VB codings:
1 Protected Sub SaveBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) 2 Try 3 4 ' PMapFU is the name of my file upload control 5 If PMapFU.HasFile = False Then 6 ' No file uploaded! 7 ImgStatus.Text = "Please first select an image to upload" 8 Else 9 ' Display the uploaded file's details 10 ImgStatus.Text = String.Format( _ 11 "Uploaded file: {0}<br />", _ 12 PMapFU.FileName) 13 14 ' Save the file 15 Dim filePath As String = Server.MapPath("~/PMap/" & PMapFU.FileName) 16 PMapFU.SaveAs(filePath) 17 SqlDataSource1.Insert() 18 End If 19 20 SaveMsg.Text = PNameTxt.Text + " is saved!" 21 Catch ex As Exception 22 SaveMsg.Text = ex.Message.ToString 23 End Try 24 25 End Sub 26I would appreciate it if codings could be given to help me (=
Thanks for all the help in advance!!!
Warm Regards,
Sam
Cheers,
Sam
![]() |
0 |
![]() |
With regards in saving image path to a temp folder
http://www.vbforums.com/showthread.php?p=2268392
With regards in displaying images to gridview
http://www.koffeekoder.com/ArticleDetails.aspx?id=207
Regards,Vinz
"Code, Beer and Music" that's my way of being a programmer!
How to get your Forum Question Answered | Blog | CodeASP.NET
![]() |
1 |
![]() |
Hi
You just save the file name in database so that you can retrieve the imagefilename and bind it to any data display controls like datalist or grid view.
you can save filename in database with relation to userid i.e have 2 columns in database
ImageName nvarchar(20),
user id int not null..
when ur saving the file in Image folder Pmap ,use this format
lbluploadresume.Text=Session["uid"].ToString()+"_"+Path.GetFileName(File1.PostedFile.FileName);
if(File1.PostedFile!=null){
File1.PostedFile.SaveAs(Server.MapPath("~/Documents/"+lbluploadresume.Text));
///s=Server.MapPath("~/Documents/"+Label1.Text.ToString());}
and save the lbluploadresume.text in database so that you with the uploaded filename userid will be attached so that if u have same filename for different users the new file will replace the old one.
when Your are retreiving the Image use this code
<Img src='Foldername/<%#DataBinder.Eval(Container.DataItem,"ImageName")%>'
Happy Programming
Shashi.
![]() |
0 |
![]() |
hmmm, i don't noe how lehz...
but please juz mark me as answers can liao =)
btw...,
Computers will take the place of books.Becoz you doesn't have to reach a high shelf anymore! =)
~ Jabalang Lahz
Regards,
Jabalang
--------------------------------------------------
Thanks for building the Internet as a Library.
Thanks for sharing and being helpful.
![]() |
0 |
![]() |
u can make use of datalist
<asp:DataList ID="dtlMemberDetails" CssClass="style7" runat="server" BackColor="#b3af99"
BorderColor="White" BorderStyle="None" BorderWidth="2px" CellPadding="6" CellSpacing="6" Width="100%">
<HeaderTemplate>
</HeaderTemplate>
<FooterTemplate>
</FooterTemplate>
<ItemTemplate>
<TABLE cellpadding="10" cellspacing="0">
<TBODY>
<TR valign="top">
<TD style="WIDTH: 120px,height=150" rowSpan=2 valign="Top" align="left"><%# Comment.ShowImage(Eval("MemberName").ToString().Trim()+".jpg","80","100")%></TD>
</TR>
</TBODY>
</TABLE>
</ItemTemplate>
<FooterStyle BackColor="#B3AF99" ForeColor="Black" />
<SelectedItemStyle BackColor="#B3AF99" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#B3AF99" Font-Bold="True" ForeColor="#E7E7FF" />
<ItemStyle BackColor="#BFBBA8" ForeColor="Black" BorderColor="White" />
</asp:DataList>
==================================
public static string ShowImage(string file, string width, string height)
{
string strReturn = "";
string filePath;
if (System.IO.File.Exists(HttpContext.Current.Request.PhysicalApplicationPath + "MemberPics\\" + file))
{
filePath = CommonClass.GetSiteRoot() + "/MemberPics/" + file ;
}
else
{ filePath = CommonClass.GetSiteRoot() + "/MemberPics/" + "photo.jpg"; }
strReturn = "<img src='" + filePath + "' border='0' width='" + width + "' height=" + height + ">";
return strReturn;
}
====================================
public static string GetSiteRoot()
{
string port = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
if (port == null || port == "80" || port == "443")
port = "";
else
port = ":" + port;
string protocol = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
if (protocol == null || protocol == "0")
protocol = "http://";
else
protocol = "https://";
string strOut = protocol + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port + System.Web.HttpContext.Current.Request.ApplicationPath;
return strOut;
}
![]() |
0 |
![]() |
Hey All!
Thanks for all the help rendered (=
I would esp like to thank vinz for his second link and shashikantht for his post which proved rather helpful to me (=
However, after several attempts, I still could not get the problem solved! So I would appreciate it if more help can be offered!!!
I did some research myself and came across the following article (please take a look at it if you have the time):
http://www.beansoftware.com/ASP.NET-Tutorials/Manipulating-Files-Directories.aspx
Please look at the topic 'A practical example of file and directory manipulation in ASP.NET' in the article.
It is what I have been wanting to do, but I just couldn't get the desired results after several attempts.
Is there any way I can make use of the codings in the article and incoporate them into my existing codes to make things work?
I just want to insert the image path into my sql database so that it can be retrieved later according to its respective imgID!!!
URGENT HELP NEEDED!!!
Thanks for all the effort in advance!!!
Cheers,
Sam
Cheers,
Sam
![]() |
0 |
![]() |
Up you go!
Regards,
Jabalang
--------------------------------------------------
Thanks for building the Internet as a Library.
Thanks for sharing and being helpful.
![]() |
0 |
![]() |
say somemore...
Regards,
Jabalang
--------------------------------------------------
Thanks for building the Internet as a Library.
Thanks for sharing and being helpful.
![]() |
0 |
![]() |
Hi All!
Thanks for all your help!!!
I have managed to solve the error on my own (=
So here is my FINAL solution for inserting image path and other data into the database... Once this is done, displaying wasn't a problem after that.
My VB codes are as follows:
1 Protected Sub SaveBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
2 Try
3
4 Dim filename As String = ""
5 Dim filecontent As String = ""
6 Dim path As String = Server.MapPath("~/PMap/Main/" & PMapFU.FileName)7 Dim allowExtension As String() = {".gif", ".png", ".jpeg", ".jpg", ".bmp"}
8
9
10 If PMapFU.HasFile = False Then
11 ' No file uploaded!
12 ImgStatus.Text = "Please first select an image to upload"
13
14 Else
15 Dim fileExtension As String = System.IO.Path.GetExtension(PMapFU.FileName).ToLower
16 Dim i As Integer
17
18 'Check file type
19 For i = 0 To allowExtension.Length - 1
20 If fileExtension = allowExtension(i) Then
21
22 ' Save the file in folder
23 PMapFU.PostedFile.SaveAs(path)
24
25 ' Display the uploaded file's details
26 ImgStatus.Text = String.Format( _
27 "Uploaded file: {0}<br />", _
28 PMapFU.FileName)
29 End If
30 Next
31 End If
32
33 Dim cnn As Data.SqlClient.SqlConnection
34 Dim cmd As Data.SqlClient.SqlCommand
35 Dim param As Data.SqlClient.SqlParameter
36 Dim strSQL As String
37
38 strSQL = "Insert Into MainProcess( ProcessName, ProcessPGU, EntityBelongTo, TeamBelongTo, ProcessOwner, CurrentVersion, VersionDate, FirstIssueDate, PreparedBy, ApprovedBy, Purpose, Description, ProcessMap) Values( @ProcessName, @ProcessPGU, @EntityBelongTo, @TeamBelongTo, @ProcessOwner, @CurrentVersion, @VersionDate, @FirstIssueDate, @PreparedBy, @ApprovedBy, @Purpose, @Description, @ProcessMap)"
39
40 Dim connString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\Processes.mdf"";Integrated Security=True;User Instance=True"
41 cnn = New Data.SqlClient.SqlConnection(connString)
42
43 cmd = New Data.SqlClient.SqlCommand(strSQL, cnn)
44
45 param = New Data.SqlClient.SqlParameter("@ProcessName", Data.SqlDbType.NVarChar)
46 param.Value = PNameTxt.Text
47 cmd.Parameters.Add(param)
48
49 param = New Data.SqlClient.SqlParameter("@ProcessPGU", Data.SqlDbType.NVarChar)
50 param.Value = PPGUDDL.SelectedValue
51 cmd.Parameters.Add(param)
52
53 param = New Data.SqlClient.SqlParameter("@EntityBelongTo", Data.SqlDbType.NVarChar)
54 param.Value = EntityDDL.SelectedValue
55 cmd.Parameters.Add(param)
56
57 param = New Data.SqlClient.SqlParameter("@TeamBelongTo", Data.SqlDbType.NVarChar)
58 param.Value = TeamDDL.SelectedValue
59 cmd.Parameters.Add(param)
60
61 param = New Data.SqlClient.SqlParameter("@ProcessMap", Data.SqlDbType.VarChar)
62 param.Value = "~/PMap/Main/" & PMapFU.FileName
63 cmd.Parameters.Add(param)
64
65 param = New Data.SqlClient.SqlParameter("@ProcessOwner", Data.SqlDbType.NVarChar)
66 param.Value = POwnerTxt.Text
67 cmd.Parameters.Add(param)
68
69 param = New Data.SqlClient.SqlParameter("@CurrentVersion", Data.SqlDbType.Decimal)
70 param.Value = CurrentVerTxt.Text
71 cmd.Parameters.Add(param)
72
73 param = New Data.SqlClient.SqlParameter("@VersionDate", Data.SqlDbType.DateTime)
74 param.Value = VerDateTxt.Text
75 cmd.Parameters.Add(param)
76
77 param = New Data.SqlClient.SqlParameter("@FirstIssueDate", Data.SqlDbType.DateTime)
78 param.Value = FIDateTxt.Text
79 cmd.Parameters.Add(param)
80
81 param = New Data.SqlClient.SqlParameter("@PreparedBy", Data.SqlDbType.NVarChar)
82 param.Value = PreparedByTxt.Text
83 cmd.Parameters.Add(param)
84
85 param = New Data.SqlClient.SqlParameter("@ApprovedBy", Data.SqlDbType.NVarChar)
86 param.Value = ApprovedByTxt.Text
87 cmd.Parameters.Add(param)
88
89 param = New Data.SqlClient.SqlParameter("@Purpose", Data.SqlDbType.NVarChar)
90 param.Value = PurposeTxt.Text
91 cmd.Parameters.Add(param)
92
93 param = New Data.SqlClient.SqlParameter("@Description", Data.SqlDbType.NVarChar)
94 param.Value = DescTxt.Text
95 cmd.Parameters.Add(param)
96
97
98 cnn.Open()
99 cmd.ExecuteNonQuery()
100 cnn.Close()
101
102 SaveMsg.Text = PNameTxt.Text + " is saved!"
103 Return
104 Catch ex As Exception
105 SaveMsg.Text = ex.Message.ToString
106 End Try
107
108 End Sub
109
Cheers,
Sam
![]() |
0 |
![]() |
Hi SummerBreez, can you post the browse file uploadcode that matches the above code behind? I'd love to look at it!! Thanks!
![]() |
0 |
![]() |