The business of file uploading and downloading
At work, everyday there are some people need to send some files to some people, they do not want to use email, rather, they want to upload then their clients download, with password protection, with email notification.
All right, a small application to serve the purpose:
File Upload (allow multiple files)
<%@ Page Language="C#" %>
<!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>Untitled Page</title>
<script runat="server">
void UploadMultipleFiles_Clicked(object Sender, EventArgs e)
{
//Variable to hold the result
string m_strResultMessage ="";
//Variable to hold the FileName
string m_strFileName;
//Variable FolderName where the files will be saved
string m_strFolderName = "D:\\webs\\TempMultipleFiles\\";
//Variable to hold the File
HttpPostedFile m_objFile;
//Variable used in the Loop
int i;
try {
//Loop Through the Files
for (i = 0; i <= Request.Files.Count - 1; i++) {
//Get the HttpPostedFile
m_objFile = Request.Files
;
//Check that the File exists has a name and is not empty
if (!(m_objFile == null | m_objFile.FileName == "" | m_objFile.ContentLength < 1)) {
//Get the name of the file
m_strFileName = m_objFile.FileName;
m_strFileName = System.IO.Path.GetFileName(m_strFileName);
//Creates the folder if it does not exists
if ((!System.IO.Directory.Exists(m_strFolderName)))
{
System.IO.Directory.CreateDirectory(m_strFolderName);
}
//Save each uploaded file
m_objFile.SaveAs(m_strFolderName + m_strFileName);
//Assign the File Name and File Type to Result
m_strResultMessage = m_strResultMessage + "Uploaded File: " + m_objFile.FileName + " of type " + m_objFile.ContentType + " <br> ";
//Hide the Multiple Form Upload Panel
MultipleFileUploadForm.Visible = false;
}
}
//If no files where selected provide a user friendly message
if (m_strResultMessage == "") {
m_strResultMessage = "Select atleast one file to upload.";
}
}
catch (Exception errorVariable) {
//Trap the exception
m_strResultMessage = errorVariable.ToString();
}
//Unhide the Result Label
ResultMsg.Visible = true;
//Assign the Result to ResultMsg Label Text
ResultMsg.Text = m_strResultMessage;
}
</script>
</head>
<body>
<ASP:PANEL id="MultipleFileUploadForm" runat="server" visible="true">
<FORM id="Form2" method="post" encType="multipart/form-data" runat="server">
<H1> Select the Files to Upload to the Server:
</H1>
<P>
<BR>
<INPUT id="File1" type="file" name="File1" runat="server">
</P>
<P>
<INPUT id="File2" type="file" name="File2" runat="server"></P>
<P>
</P>
<P>
<INPUT id="File3" type="file" name="File1" runat="server">
</P>
<P>
<INPUT id="File4" type="file" name="File1" runat="server">
</P>
<P>
<INPUT id="File5" type="file" name="File1" runat="server">
</P>
<INPUT id="Submit1" type="submit" value="Upload Files" name="Submit1" runat="server" OnServerClick="UploadMultipleFiles_Clicked">
</FORM>
</ASP:PANEL>
<ASP:LABEL id="ResultMsg" runat="server" Visible="False" ForeColor="#ff0033"></ASP:LABEL>
</body>
</html>
File Download
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public void Page_Load(object sender, EventArgs e)
{
string root = @"D:\webs\TempMultipleFiles\";
string filename = Request.Params["file"];
if ((filename != null))
{
string filepath = root + filename;
if (File.Exists(filepath))
{
//
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
Response.Flush();
Response.WriteFile(filepath);
}
else
{
Response.Write("not ex");
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title> file download</title>
</head>
<body>
</body>
</html>