Form tag:
<form method="post" runat="server" enctype="multipart/form-data">
File input:
<input type="file" size="34" id="tbUpload" runat="server" />
C# server side file uploader
Code:
public static void UploadFile(HttpPostedFile file, string type, string destination, bool canOverwrite)
{
HttpPostedFile uFile = file;
if (uFile == null || uFile.ContentLength == 0 || uFile.FileName == "") throw new Exception("No file has been selected!");
if (type != "")
if (uFile.ContentType != type)
throw new ArgumentException("Type does not match! Allowed: " + uFile.ContentType);
int len = uFile.ContentLength;
byte[] data = new byte[len];
uFile.InputStream.Read(data, 0, len);
if (File.Exists(destination))
{
if (canOverwrite) File.Delete(destination);
else throw new Exception("File already exists!");
}
FileStream newFile = File.Create(destination);
newFile.Write(data, 0, data.Length);
newFile.Close();
}
Bookmarks