How to use HTML input control to upload files

I had control over the HTML input to upload the file, but the file is returned empty.

<input type="file" class="upload"  runat="server" id="FUFile"/>

string tempVar = "~/res/Posts/" + FUFile.Value.ToString();
        FUFile.ResolveUrl(Server.MapPath(tempVar));
+4
source share
2 answers

The file is sent from the file. If you need to save FUFile.PostedFile:

if (FUFile.PostedFile != null)
{
    string tempVar = "~/res/Posts/" + FUFile.Value.ToString();
    FUFile.PostedFile.SaveAs(Server.MapPath(tempVar));
}

Here you can check it out:

In the markup, I have this:

<input type="file" class="upload"  runat="server" id="FUFile"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

In the code, I have this method:

protected void Button1_Click(object sender, EventArgs e)
{
    if (FUFile.PostedFile != null)
    {
        string tempVar = "~/res/Posts/" + FUFile.Value.ToString();
        FUFile.PostedFile.SaveAs(Server.MapPath(tempVar));
    }
}

When I select the file and click the button, it uploads the file to the .. / res / Posts folder.

enter image description here

+1
source

Just use the FileUpload control

<asp:FileUpload runat="server" ID="FUFile">
<asp:Button runat="server" ID="UploadButton" Text="Upload file" OnClick="UploadButton_Click"/>

FUFile (FileContent , FileBytes , PostedFile HttpPostedFile, SaveAs), .

, . : #?

. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload(v=vs.110).aspx

+1

All Articles