1

I'm trying to make a simple uploadfile control with ASP.NET, and it wouldnt work:

Here's my code(.aspx):

<form id="form1" runat="server">
  <div>
    upload a file now.
      <asp:FileUpload ID="fileupload1" runat="server" />
      <asp:Button  ID="button1"  Text="Upload"  runat="server"  Width="73px" 
            onclick="button1_Click" />
    <asp:Label ID="Label1" runat="server"  Font-Bold="True"  ForeColor="#000099">
         </asp:Label>
  </div>  
</form>

and here's my code behind(.cs):

if(fileupload1.HasFile)
{
    try
    {
        if(fileupload1.PostedFile.ContentType ==  "image/jpeg")
        {
            if(fileupload1.PostedFile.ContentLength < 51200000)
            {
               string  filename = Path.GetFileName(fileupload1.FileName);
               fileupload1.SaveAs(Server.MapPath("~/img/") + filename);
               Label1.Text ="File uploaded successfully!";
            }
            else
                Label1.Text ="File maximum size is 500 Kb";
        }
        else
            Label1.Text ="Only JPEG files are accepted!";
    }
    catch(Exception exc)
    {
        Label1.Text = "The file could not be uploaded. The following error occured: "
                           + exc.Message;
    }
  }

the file is not presented in the server.. any thoughts?

when I breakpoint, they all goes valid, the application gets to the code, it all working , but won't save it to the folders.

10
  • 1
    What happens if you put breakpoints on the if statements, are they valid? Commented Mar 10, 2013 at 9:26
  • yes, they are all valid, it goes through the code but wouldnt present it in the folders for some reason Commented Mar 10, 2013 at 9:32
  • no exceptions at all. Commented Mar 10, 2013 at 9:34
  • What does Server.MapPath("~/img/") + filename resolve to? Commented Mar 10, 2013 at 9:35
  • 2
    You need to include enctype="multipart/form-data" as a form attribute.. otherwise nothing will be uploaded. Commented Mar 10, 2013 at 9:35

3 Answers 3

2

This may or may not work entirely, but you need to include an enctype attribute in your form.

<form id="form1" runat="server" enctype="multipart/form-data">

If you don't do that, browsers won't transfer the file.

See here: https://developer.mozilla.org/en-US/docs/HTML/Element/form#attr-enctype

Sign up to request clarification or add additional context in comments.

1 Comment

1

change

fileupload1.SaveAs(Server.MapPath("~/img/") + filename);

with

fileupload1.PostedFile.SaveAs(Server.MapPath("~/img/") + filename);

3 Comments

@thormayer if you debug with fiddler do you see the file stream?
when I debug in VS, I can see the file in the debug.
@thormayer do you check the folder permissions?
0

I think the problem lies in these two lines

string  filename = Path.GetFileName(fileupload1.FileName);
fileupload1.SaveAs(Server.MapPath("~/img/") + filename);

why are you using

 string  filename = Path.GetFileName(fileupload1.FileName);

It should be simple

fileupload1.SaveAs(Server.MapPath("~/img/") + fileupload1.FileName);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.