Show Changes Show Changes
Edit Edit
Print Print
Recent Changes Recent Changes
Subscriptions Subscriptions
Lost and Found Lost and Found
Find References Find References
Rename Rename
Administration Page Administration Page
Search

History

7/4/2008 11:12:58 AM
207.35.224.2
6/30/2008 6:39:07 AM
203.177.74.136
10/10/2005 1:53:05 PM
-62.3.119.54
6/26/2005 8:01:02 PM
-128.227.27.229
5/12/2005 8:25:43 AM
-12.11.157.254
List all versions List all versions

RSS feed for the FlexWiki namespace

Wiki Upload Page V2
.
Summary
Author
JustinCollum, 1 Sep 04
Nearby

Important: this page will look for this in your web.config (won't work without it)

                <appSettings>
                <!-- this key defines the directory (off of the site root) 
                        that the upload code will place uploaded files -->
                 <add key="UploadDirectory" value="content\upload" />
                 </appSettings> 

The locations that the upload page puts files are hardcoded (images/, docs/, etc.) and organized according to the extension on your uploaded file.

In addition, you will need to re-compile the entire solution in VS.NET (after adding the upload.aspx and upload.aspx.cs files, of course) or place a src="upload.aspx.cs" directive at the top of the aspx page (i.e. - inside the first line of upload.aspx: <%@ Page ... %>).

upload.aspx

        <%@ Page language="c#" AutoEventWireUp="false" Codebehind="upload.aspx.cs" Inherits="FlexWiki.Upload.FileUpload" %>
        <HTML>
        <HEAD>
                <%= InsertStylesheetReferences() %>
                <LINK href="wiki.css" type="text/css" rel="stylesheet">
                        <LINK href="wiki.css" type="text/css" rel="stylesheet">
        </HEAD>
        <body>
                <FORM id="Form1" method="post" encType="multipart/form-data" runat="server">
                        <TABLE id="Table2" cellPadding="10" border="0" width="80%" align="center">
                                <TR>
                                        <TD id="BorderRight" bgColor="lightsteelblue" colSpan="2">
                                                <DIV class="StaticTopicBar" id="Div" style="DISPLAY: block" leftMargin="40" rightMargin="40"><FONT color="#000000"><STRONG>Upload 
                                                                        A File</STRONG></FONT></DIV>
                                        </TD>
                                </TR>
                                <TR>
                                        <TD colSpan="2">
                                                <P align="left"><INPUT id="filMyFile" type="file" size="60" name="filMyFile" runat="server">   
                                                        <asp:button id="cmdSend" runat="server" Text="Send"></asp:button></P>
                                        </TD>
                                </TR>
                                <TR>
                                        <TD colSpan="2">
                                                <P>Pictures (.jpg/.gif/.png) are put in the <B>/upload/images/</B> directory.</P>
                                                <P>Html (.htm/.html) are put in the <B>/upload/html/</B> directory.</P>
                                                <P>Word files (.doc) are put in the <B>/upload/doc/</B> directory.</P>
                                                <P>All other files are put in the <B>/upload/files/</B> directory.</P>
                                        </TD>
                                </TR>
                                <TR>
                                        <TD colSpan="2">
                                                <P>
                                                        <asp:Label id="lblInfo" runat="server" Font-Bold="True" Visible="false"></asp:Label></P>
                                                <P>
                                                        <asp:Label id="lblLocation" runat="server" Visible="False"></asp:Label></P>
                                                <P>
                                                        <asp:Label id="lblErrorMessage" runat="server" Visible="False"></asp:Label></P>
                                                <P>
                                                        <asp:Label id="lblText1" runat="server"></asp:Label></P>
                                                <P>
                                                        <asp:Label id="lblLinkToHome" runat="server" Visible="False"><a href="../default.aspx">Return 
                                                                        to HomePage</a> or press the back button.</asp:Label></P>
                                        </TD>
                                </TD>
                                <TR>
                                        <TD align="center" colSpan="2">
                                                <asp:Image id="imgFile" runat="server" Visible="False"></asp:Image>
                                        </TD>
                                </TR>
                        </TABLE>
                        </TABLE>
                </FORM>
        </body>
        </HTML>

upload.aspx.cs

                         /*
        This page is for use with FlexWiki but can be adapted to other things
        Modified from existing code
        Original author: WouterCX
        Refactored by: JustinCollum
        Please note that this page will look for this in your web.config (won't work without it)


                        <appSettings>
                        <!-- this key defines the directory        (off of        the        site root) 
                                that the upload        code will place        uploaded files -->
                        <add key="UploadDirectory"        value="content\upload" />
                        </appSettings>
                //*/
                using System;
                using System.Web;
                using System.Web.UI;
                using System.Web.UI.WebControls;
                using System.Web.UI.HtmlControls;
                using System.IO;
                using System.Data;
                using System.Data.OleDb;
                using System.Text.RegularExpressions;
                using System.Configuration;
        namespace FlexWiki.Upload
        {
                ///        <summary>
                ///        Summary        description        for        WebForm1.
                ///        </summary>
                public class FileUpload        : System.Web.UI.Page
                {
                        protected System.Web.UI.WebControls.Image imgFile;
                        protected System.Web.UI.WebControls.Label lblErrorMessage;
                        protected System.Web.UI.WebControls.Label lblInfo;
                        protected System.Web.UI.WebControls.Label lblText1;
                        protected System.Web.UI.HtmlControls.HtmlInputFile filMyFile;
                        protected System.Web.UI.WebControls.Button cmdSend;
                        protected System.Web.UI.WebControls.Label lblLocation;
                        protected System.Web.UI.WebControls.Label lblLinkToHome;
                        protected Label        lblFile;
                        override protected void        OnInit(EventArgs e)
                        {
                                InitializeComponent();
                                base.OnInit(e);
                        }
                        private        void InitializeComponent()
                        {         
                                this.cmdSend.Click += new System.EventHandler(this.cmdSend_Click);
                                this.Load += new System.EventHandler(this.Page_Load);
                        }
                        private        void Page_Load(object sender, System.EventArgs e)
                        {
                                if (!Page.IsPostBack)
                                {


                                }
                        }
                        // Processes click on our cmdSend button
                        private        void cmdSend_Click(object sender, System.EventArgs e)
                        {
                                try
                                {
                                        // Check to        see        if file        was        uploaded
                                        if(        filMyFile.PostedFile !=        null )
                                        {
                                                // clear the error message
                                                lblErrorMessage.Text = ""; 
                                                lblErrorMessage.Visible        = false; 
                                                lblLinkToHome.Visible =        false; 
                                                lblInfo.Visible        = false; 
                                                lblLocation.Visible        = false; 
                                                // Get a reference to PostedFile object
                                                HttpPostedFile myFile =        filMyFile.PostedFile;
                                                // Get size        of uploaded        file
                                                int        nFileLen = myFile.ContentLength; 
                                                // make        sure the size of the file is > 0
                                                if(        nFileLen > 0 )
                                                {
                                                        // Allocate        a buffer for reading of        the        file
                                                        byte[] myData =        new        byte[nFileLen];
                                                        string strDirectoryToWriteTo;
                                                        string strWriteToFile;
                                                        string strFilename;
                                                        // Read        uploaded file from the Stream
                                                        myFile.InputStream.Read(myData,        0, nFileLen);
                                                        strDirectoryToWriteTo =        ReturnDirectoryToWriteTo(myFile.FileName);
                                                        // Create a        name for the file to store
                                                        strFilename        = Path.GetFileName(myFile.FileName); //
                                                        // Write data into a file
                                                        strWriteToFile = Server.MapPath(strFilename);
                                                        // Get the current virtual directory, mapped to        a path
                                                        strWriteToFile = Server.MapPath("")        + "\\" + strDirectoryToWriteTo + "\\" +        strFilename;
                                                        // fileNumber var is the one digit number that is added        to a file name if it exists
                                                        int        fileNumber = 0;
                                                        string fileNameAdder = "";
                                                        string newFileName = "";


                                                        // append a        three digit        int        to the file        name to        make it        unique
                                                        while (File.Exists(strWriteToFile))
                                                        {
                                                                if (fileNumber > 9)
                                                                {
                                                                        throw new System.IO.IOException("Too many copies of        file exist on server.");
                                                                }
                                                                fileNumber++;
                                                                fileNameAdder =        ("-" + fileNumber.ToString("0")+ ".");
                                                                newFileName        = strWriteToFile.Replace(".",fileNameAdder);


                                                                if (File.Exists(newFileName))
                                                                {
                                                                        // if the new file name        is taken start over
                                                                        continue;
                                                                }
                                                                else
                                                                {
                                                                        // if the new file name        is not taken, terminate        the        loop
                                                                        strWriteToFile = newFileName; 
                                                                        break;
                                                                }
                                                        }


                                                        // write the file out
                                                        WriteToFile(strWriteToFile,        ref        myData);
                                                        // get the name        of the file
                                                        string fileName        = OnlyFileName(strWriteToFile);


                                                        // Set label's text
                                                        lblInfo.Text = 
                                                                "Filename: " + fileName        + "<br />" + 
                                                                "Size: " + nFileLen.ToString("0,0")        + "        bytes.<br/><p />";
                                                        // show        the        location
                                                        lblLocation.Text = "<p/>Relative path: <p/>" +        
                                                                @"../" + strDirectoryToWriteTo.Replace(@"\", @"/") + @"/" +        fileName + "<p/>" +
                                                                "Please        note the location and file name        before leaving this        page.";
                                                        // show        the        full path
                                                        lblText1.Text =        "URL path:<p />"
                                                                + RootUrl(Request) + strDirectoryToWriteTo.Replace(@"\",@"/") +        "/"        + OnlyFileName(strWriteToFile);
                                                        lblText1.Visible = true;
                                                        // Set URL of the the object to        point to the file we've        just saved
                                                        if (IsImageFile(strWriteToFile))
                                                        {
                                                                //lblText1.Text        = "File        available at:<br>" + strWriteToFile;
                                                                //lblText1.Visible = true;
                                                                imgFile.ImageUrl =        RootUrl(Request) + strDirectoryToWriteTo.Replace(@"\",@"/")        + "/" +        OnlyFileName(strWriteToFile);
                                                                imgFile.ToolTip        = "This        file was stored        as file.";


                                                                // show        the        images and text
                                                                imgFile.Visible        = true;
                                                                lblText1.Visible = true;
                                                        }


                                                        lblLocation.Visible        = true;        
                                                        lblInfo.Visible        = true;
                                                        lblLinkToHome.Visible =        true; 
                                                }


                                        }
                                }
                                catch (System.IO.IOException)
                                {
                                        lblErrorMessage.Text = "There are too many files on        the        server with        that name. Please rename it        and        upload again.";
                                        lblErrorMessage.ForeColor =        System.Drawing.Color.Red;
                                        lblErrorMessage.Visible        = true;        
                                }
                        }
                        private        static bool        IsImageFile(string strFullPath)
                        {
                                bool blnIsImage        = false;
                                strFullPath        = strFullPath.ToUpper();
                                if ((strFullPath.IndexOf(".JPG") > 0) || (strFullPath.IndexOf(".GIF") >        0) || (strFullPath.IndexOf(".PNG")>0))
                                {
                                        blnIsImage = true;
                                }
                                return blnIsImage;
                        }
                        private        static string OnlyFileName (string strFullPath)
                        {
                                string strImageFileName;
                                int        intLastIndex;
                                intLastIndex = strFullPath.LastIndexOf("\\")+1;
                                strImageFileName = strFullPath.Substring(intLastIndex,strFullPath.Length-intLastIndex);
                                return strImageFileName;
                        }
                        public static String Replace(String        strText,String strFind,String
                                strReplace)
                        {
                                int        iPos=strText.IndexOf(strFind);
                                String strReturn="";
                                while(iPos!=-1)
                                {
                                        strReturn+=strText.Substring(0,iPos) + strReplace;
                                        strText=strText.Substring(iPos+strFind.Length);
                                        iPos=strText.IndexOf(strFind);
                                }
                                if(strText.Length>0)
                                        strReturn+=strText;
                                return strReturn;
                        }
                        // Writes file to current folder
                        private        void WriteToFile(string        strPath, ref byte[]        Buffer)
                        {
                                // Create a        file
                                //Response.Write (strPath);
                                FileStream newFile = new FileStream(strPath, FileMode.Create);
                                // Write data to the file
                                newFile.Write(Buffer, 0, Buffer.Length);
                                // Close file
                                newFile.Close();
                        }
                        // Generates database connection string
                        private        string ReturnDirectoryToWriteTo(string strFileName)
                        {
                                // get the root        directory for uploaded files
                                string strRootUploadDir        = ConfigurationSettings.AppSettings["UploadDirectory"];        
                                string strReturnDirectory;
                                strReturnDirectory = "";
                                strFileName        = strFileName.ToLower();
                                if ((strFileName.EndsWith(".jpg"))||(strFileName.EndsWith(".gif")))
                                {
                                        strReturnDirectory=        strRootUploadDir + "\\images";
                                }
                                else if        ((strFileName.EndsWith(".htm"))||(strFileName.EndsWith(".html")))
                                {
                                        strReturnDirectory=        strRootUploadDir + "\\html";
                                }
                                else if        (strFileName.EndsWith(".doc"))
                                {
                                        strReturnDirectory=        strRootUploadDir + "\\doc";
                                }
                                else
                                {
                                        strReturnDirectory=        strRootUploadDir + "\\files";
                                }
                                return strReturnDirectory;
                        }
                        // Reads the name of current web page
                        private        string GetMyName()
                        {
                                // Get the script name
                                string strScript = Request.ServerVariables["SCRIPT_NAME"];
                                // Get position        of last        slash
                                int        nPos = strScript.LastIndexOf("/");
                                // Get everything after        slash
                                if(        nPos > -1 )
                                        strScript =        strScript.Substring(nPos + 1);
                                return strScript;
                        }
                        // Reads the servername        of current webserver
                        private        string GetMyServerName()
                        {
                                // Get the script name
                                string strServer = Request.ServerVariables["SERVER_NAME"];
                                // Get position        of last        slash
                                int        nPos = strServer.LastIndexOf("/");
                                // Get everything after        slash
                                if(        nPos > -1 )
                                        strServer =        strServer.Substring(nPos + 1);
                                return strServer;
                        }
                        protected string InsertStylesheetReferences()
                        {
                                string answer =        "<LINK href='" + RootUrl(Request) +        "wiki.css' type='text/css' rel='stylesheet'>";
                                return answer;
                        }
                        protected string RootUrl(HttpRequest req)
                        {
                                string full        = req.Url.ToString();
                                if (req.Url.Query != null && req.Url.Query.Length >        0)
                                {
                                        full = full.Substring(0, full.Length - req.Url.Query.Length);
                                }
                                if (req.PathInfo !=        null &&        req.PathInfo.Length        > 0)
                                {
                                        full = full.Substring(0, full.Length - (req.PathInfo.Length        + 1));
                                }
                                full = full.Substring(0, full.LastIndexOf('/') + 1);
                                return full;
                        }
                }
        }

Sorry for the absence of vertical white space. It's easier to cut and paste into flexwiki if it isn't there.

Note that the sub folders (images, files, doc, html) have to exist already, the script doesn't create them automatically. -- Green Dragon

Note also that the upload folder itself must be manually created. Note also that while the upload page claims to handle png, the code doesn't explicitly deal with that extension, so they end up in the files directory if you don't edit this. I also had to change lblLinkToHome to simply default.aspx instead of referring to the parent directory. Perhaps depends on where you put the upload.aspx page. I also don't know if content\upload is supposed to be there automatically, but I didn't have a content directory so I just used upload and created an upload directory. -- Tom Stewart

Note
private		string ReturnDirectoryToWriteTo(string strFileName)
{
		// get the root		directory for uploaded files
		// you should pre-create the folders under your upload folder (i.e., \images, \html, \doc, \xls)
		string strRootUploadDir		= ConfigurationSettings.AppSettings["UploadDirectory"];
		string strReturnDirectory;
		strReturnDirectory = "";
		strFileName		= strFileName.ToLower();
		if ((strFileName.EndsWith(".jpg"))||(strFileName.EndsWith(".gif"))||(strFileName.EndsWith(".png")))
		{
				strReturnDirectory= strRootUploadDir + "\\images";
		}
		else if ((strFileName.EndsWith(".htm"))||(strFileName.EndsWith(".html")))
		{
				strReturnDirectory= strRootUploadDir + "\\html";
		}
		else if ((strFileName.EndsWith(".doc"))||(strFileName.EndsWith(".rtf")))
		{
				strReturnDirectory= strRootUploadDir + "\\doc";
		}
		else if (strFileName.EndsWith(".xls"))
		{
				strReturnDirectory= strRootUploadDir + "\\xls";
		}
		else if ((strFileName.EndsWith(".ppt"))||(strFileName.EndsWith(".pps")))
		{
				strReturnDirectory= strRootUploadDir + "\\ppt";
		}
		else if (strFileName.EndsWith(".mdb"))
		{
				strReturnDirectory= strRootUploadDir + "\\mdb";
		}
		else if (strFileName.EndsWith(".pdf"))
		{
				strReturnDirectory= strRootUploadDir + "\\pdf";
		}
		else if (strFileName.EndsWith(".exe"))
		{
				strReturnDirectory= strRootUploadDir + "\\exe";
		}
		else if ((strFileName.EndsWith(".txt"))||(strFileName.EndsWith(".ini"))||(strFileName.EndsWith(".log"))||(strFileName.EndsWith(".asc"))||(strFileName.EndsWith(".inf")))
		{
				strReturnDirectory= strRootUploadDir + "\\txt";
		}
		else
		{
				strReturnDirectory= strRootUploadDir + "\\files";
		}
		return strReturnDirectory;
}
Not logged in. Log in

Welcome to the home of FlexWiki, an experimental collaboration tool, based on WikiWiki.

This is FlexWiki, an open source wiki engine.

This site supports the new NoFollow anti-spam initiative.
Change Style

Recent Topics