Click here to Skip to main content
15,915,867 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi All,

I have written a code to upload image in a specific folder. The coding is
VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If FileUpload1.HasFile Then
           Try
               Dim filename As String = Path.GetFileName(FileUpload1.FileName)
               FileUpload1.SaveAs(Server.MapPath("~/Images") & filename)
               StatusLabel.Text = "Upload status: File uploaded!"
           Catch ex As Exception
               StatusLabel.Text = "Upload status: The file could not be uploaded."
           End Try
       End If
End Sub

And the source code is
ASP.NET
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" />

<asp:Label ID="StatusLabel" runat="server" text="Upload status: " />

But the image is not stored. Can any one help me on this?
Posted
Updated 26-Nov-13 3:36am
v2
Comments
TryAndSucceed 26-Nov-13 10:25am    
Did you debug and find where you are stuck?

Insterad of eating the exception you caught, you might want to log it to a file or the Application Event Log.

But, for now, remove the Try/Catch block from your code so you can see the error that it's throwing.

But, after a quick glance, you're appending the filename of the upload to the string "~/Images". Your appended name will look like this: "~/ImagesSomeImageIUploaded.jpg". Does that look like a valid path to you?? Read the documentation on the Path class and you'll find a method called "Combine" that will create a path string for you.
 
Share this answer
 
I think below code help u

VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim path As String
       path = Server.MapPath("~/Images/")
       If (FileUpload1.HasFile) Then
           path = path + FileUpload1.FileName
           FileUpload1.SaveAs(path)

'If you want that uploaded image...
           'Dim img As String
           'img = "~/Images/" + FileUpload1.FileName
           'Image1.ImageUrl = img
       End If
   End Sub
 
Share this answer
 
string filename=File.FileName;
XML
path = Server.MapPath("~/Images/")
<pre lang="cs">filePath = System.IO.Path.Combine(path , filename);
               // file is uploaded
               file.SaveAs(filePath);</pre>


OR

C#
string fileWithPath=Server.MapPath("~/Images"/;)+FileName.Jpg;

file.SaveAs(fileWithPath)



you need to provide name of the file with extension. Then it will get save

Hope This Helps!!!
 
Share this answer
 
if (FileUpload1.PostedFile.ContentLength != 0)
{
//filename = FileUpload1.PostedFile.FileName;
filename = FileUpload1.FileName;
path = "../Upload/" + filename;
 
Share this answer
 
v2

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