File uploading with ASP.NET HtmlForms
by johna | June 12, 2012 | ASP.NET Html Forms ASP.NET Web Forms Web Development
Following up on my previous article about ASP.NET HtmlForms, this sample code illustrates how to handle file uploads. As you can see the usual ASP.NET server side methods work perfectly well even if you don't use an ASP.NET file upload control.
Test.aspx
Test.aspx.cs
Test.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Test.Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form action="" enctype="multipart/form-data" method="post">
<input id="fupFile" name="fupFile" type="file" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
Test.aspx.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Test
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.HttpMethod == "POST")
{
HttpPostedFile file = Request.Files["fupFile"];
if (file != null && file.ContentLength > 0)
{
string filename = Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", filename)));
}
}
}
}
}
Related Posts
Converting dBase IV programs to run in the browser
by johna | September 13, 2024
Some pointless entertainment trying to get some old dBase programs running in the browser.
How to set up a debugging using the Turnkey Linux LAMP stack and VS Code
by johna | December 19, 2023
The second part in my guide to setting up a website and database using the Turnkey Linux LAMP stack.
How to set up a website and database using the Turnkey Linux LAMP stack
by johna | November 18, 2023
If you need to host your own website for the purposes of web development, Turnkey Linux LAMP Stack is an easy to install all-in-one solution that you can set up on a spare computer or a VM (Virtual Machine).
Comments
There are no comments yet. Be the first to leave a comment!