Reputation: 61
i need to request file from fileupload that is created in html. my question is quite simple how do i do this?
i know there is this option : HttpPostedFile File = Request.Files["imagem"];
but when i try to do that my File
returns NULL
.
I dont know what am i doing wrong, but even this simple code example is not working and i dont know why .
<body>
<form id="form1" runat="server">
<div>
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile File = Request.Files["imagem"];
if ( File != null)
Response.Write("Sucesso");
}
</form>
</body>
and here is my aspx code example :
<input type="file" name="image" class="image-upload" /></div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
can any one explain what am i doing wrong ? thank you
Upvotes: 0
Views: 2977
Reputation:
I'd recommend going with the asp.net file upload control:
<asp:FileUpload ID="pictureUpload" class="form-control input-sm input-spacing" runat="server" />
You can create these dynamically
FileUpload OneUpload = new FileUpload();
OneUpload.ID = "Upload" + MyIdNumber;
UploadsDiv.Controls.Add(OneUpload);
Then to access the file, you have:
pictureUpload.SaveAs(FilePathAndName);
You can also access different properties of the posted file...
pictureUpload.PostedFile.FileName
pictureUpload.PostedFile.ContentLength
pictureUpload.PostedFile.ContentType
Upvotes: 0
Reputation: 2100
Might just be a typo on here, but change your imagem
to image
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile File = Request.Files["image"];
if ( File != null)
Response.Write("Sucesso");
}
In your aspx page you need to tell the form that it will be using files. I don't think its required but its good practice to also include an id in your input that matches your name.
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<input type="file" id="image" name="image" class="image-upload" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
</body>
Upvotes: 1