4

I had HTML input control to upload file but the file returns empty.

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

string tempVar = "~/res/Posts/" + FUFile.Value.ToString();
        FUFile.ResolveUrl(Server.MapPath(tempVar));
0

2 Answers 2

1

The file is posting from the fileupload properly. If you need to save the FUFile.PostedFile :

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

Here's how you can test it:

In 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 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 a file and click the button, it uploads the file to ../res/Posts folder.

enter image description here

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

1 Comment

I had this error Object reference not set to an instance of an object
1

Just use a FileUpload control

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

You can then use FUFile's properties (FileContent for a stream, FileBytes for the full content as an array of bytes, PostedFile for the HttpPostedFile object which has the SaveAs method) as you need.

For example, see this answer for saving the stream: How do I save a stream to a file in C#?

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

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.