1and1 Help Centre Categories

print article

Allow Users to Upload Files to Webspace Using ASP.NET

Find out how to allow visitors to upload content to your package's webspace via a webpage.

The example script below assumes that the uploads will be saved to a subfolder named "uploads" relevant to the script itself. This means wherever the script is saved, a subfolder named "uploads" should be present.

Please note:
You can change the folder name if you wish to use another location. Leave a trailing double backslash (\\) at the end of the folder name.
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>
<script language="C#" runat="server">
void Button1_Click(object Source, EventArgs e){
if (Text1.Value == ""){
Span1.InnerHtml = "Error: you must enter a file name";
return;
}
if (File1.PostedFile != null){
try{
File1.PostedFile.SaveAs(Server.MapPath("uploads\\")+Text1.Value);
Span1.InnerHtml = "File uploaded successfully: " + Text1.Value;
}
catch (Exception exc){
Span1.InnerHtml = "Error saving file" + Text1.Value + "" + exc.ToString();
}
}
}
</script>
</head>
<body>
<h3>File Upload Sample</h3>
<form enctype="multipart/form-data" runat="server">
File to Upload:
<input id="File1" type="file" runat="server"><p>
Save as filename (no path): <input id="Text1" type="text" runat="server"><p>
<span id=Span1 style="font: 8pt verdana;" runat="server" /><p>
<input type=button id="Button1" value="Upload" OnServerClick="Button1_Click" runat="server">
</form>
</body>
</html>

Also, you should change file/folder permissions using Webfiles and give write permissions (both IUSR and NETWORK) for the folder where uploaded files will be saved.

For additional information, you may want to reference: