PDA

View Full Version : Asp Upload where do I start



scola
October 20th, 2009, 10:11 AM
I need to create a page that allows guests to upload files (.ppt .doc) to a folder and then also to be able to view the contents etc.

I'm assuming I'll need a database, form and folder permissions... But is that it? Can someone point me in a direction. I think I'll be all right once I get goin'

Thank you in advance

kmullings
October 20th, 2009, 04:44 PM
Hi,

You don't have to store the files in a database. You may want to consider storing them on the server. The code below has worked for me. You could enhance it by adding some sort of file compression. Let me know if it helps and please do share you enhancements.

The code below is in C#.

#region FileManager

/// <summary>
///
/// </summary>
/// <param name="subDirectory"></param>
/// <returns></returns>
internal List<string> GetFileList(string subDirectory)
{
List<string> wordDocs = (from f in (System.IO.Directory.GetFiles(Server.MapPath(Confi gurationManager.AppSettings["agentDocumentsPath"] + subDirectory + "\\"), "*.doc",
System.IO.SearchOption.AllDirectories))
select System.IO.Path.GetFileName(f)).ToList();
List<string> pdfs = (from f in (System.IO.Directory.GetFiles(Server.MapPath(Confi gurationManager.AppSettings["agentDocumentsPath"] + subDirectory + "\\"), "*.pdf",
System.IO.SearchOption.AllDirectories))
select System.IO.Path.GetFileName(f)).ToList();

return wordDocs.Union(pdfs).ToList();
}

/// <summary>
/// Return list of files
/// </summary>
/// <param name="path"></param>
/// <param name="filter"></param>
/// <returns></returns>
internal List<string> GetFileList(string path, string filter)
{
var files = from f in System.IO.Directory.GetFiles(Server.MapPath(path), filter, System.IO.SearchOption.TopDirectoryOnly)
select System.IO.Path.GetFileName(f);

return files.ToList();
}

/// <summary>
/// Download file
/// </summary>
/// <param name="path"></param>
/// <param name="fileName"></param>
/// <returns></returns>
internal ActionResult DownloadFile(string path, string fileName)
{
string pfn = Server.MapPath(path + fileName);

if (!System.IO.File.Exists(pfn))
{
throw new ArgumentException("Invalid file name or file not exists!");
}

fileName = fileName.Replace(" ", "-");

return new BinaryContentResult()
{
FileName = fileName,
ContentType = "application/octet-stream",
Content = System.IO.File.ReadAllBytes(pfn)
};
}

/// <summary>
/// Upload file
/// </summary>
/// <param name="uploadDir"></param>
/// <param name="fileName"></param>
/// <returns></returns>
internal bool UploadFile(string uploadDir, string fileName)
{
try
{
Request.Files[fileName].SaveAs(uploadDir + System.IO.Path.GetFileName(Request.Files[fileName].FileName));

return true;
}
catch (Exception exception)
{
EventLoggingDataContext errorLog = new EventLoggingDataContext();

errorLog.ErrorLog(m_user.UserID, exception.Message, exception.StackTrace);

return false;
}
}

/// <summary>
/// Makrh file as deleted
/// </summary>
/// <param name="path"></param>
/// <param name="fileName"></param>
/// <returns></returns>
internal bool DeleteFile(string path, string fileName)
{
try
{
System.IO.File.Move(path + fileName, path + DateTime.Now.ToString() + fileName);

return true;
}
catch (Exception exception)
{
EventLoggingDataContext errorLog = new EventLoggingDataContext();

errorLog.ErrorLog(m_user.UserID, exception.Message, exception.StackTrace);

return false;
}
}

#endregion

Brian
October 20th, 2009, 07:54 PM
If you are looking for an ASP solution instead of a .NET solution, you can use the AspUpload component which is on our Windows servers (sample applications (http://www.aspupload.com/manual.html)).

You will want to verify that you are only accepting uploads for .ppt and .doc files.

scola
October 21st, 2009, 10:10 AM
Excellent, Thank you.

Am I going to need permissions set to my upload folder for this to work? Also does any of this code need to be changed to reference my home path or the upload folder I created rather than c:\upload or IMG SRC="/uploaddir/ found refrenced in the two code snippets below.

/////////////////////////////////////////////////////////////////////////////
<%
Set Upload = Server.CreateObject("Persits.Upload.1")

Upload.OverwriteFiles = False
On Error Resume Next

Upload.SetMaxSize 1048576 ' Limit files to 1MB
Count = Upload.Save("c:\upload")
%>
/////////////////////////////////////////////////////////////////////////////

<% If Count > 0 Then %>
<%
Set File = Upload.Files(1)
If File.ImageType = "GIF" or File.ImageType = "JPG" or File.ImageType = "PNG" Then %>
<TD ALIGN=CENTER><IMG SRC="/uploaddir/<% = File.FileName%>"><BR><% = File.OriginalPath%><BR>
(<% = File.ImageWidth %> x <% = File.ImageHeight %> pixels)
</TD>
<% Else %>
<TD><% = File.OriginalPath %></TD>
<% End If %>
<% Else %>
<TD>File not selected.</TD>
<% End If %>

////////////////////////////////////////////////////////////////////////////////

So far my initial attempts have resulted in errors, "The system cannot find the file specified. " or similar cannot find....

Any advice is appreciated.

kmullings
October 21st, 2009, 12:07 PM
Here is another article for you...

http://msdn.microsoft.com/en-us/library/aa479405.aspx

Brian
November 6th, 2009, 09:15 AM
Scola:

Yes, you would need to change the path to your file location. That is why you would get that message.

>Count = Upload.Save("c:\upload")