Show Changes Show Changes
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

1/24/2008 9:14:33 AM
FLWCOM-jwdavidson
1/24/2008 9:13:32 AM
FLWCOM-jwdavidson
10/4/2007 5:28:28 PM
-60.218.98.94
6/12/2006 1:34:24 AM
-83.247.59.242
5/29/2006 9:45:11 PM
-203.129.207.185
List all versions List all versions

RSS feed for the FlexWiki namespace

Wiki Upload Page Script
.

sitacel

Summary

Another version: I've created another version here: WikiUploadPageV2 -- JustinCollum, 1 Sep 04

Save this as "upload.aspx" and put it in the main wiki directory, near the other files (like wikiedit.aspx).

 <%@ Page language="c#" AutoEventWireUp="false" Codebehind="upload.aspx.cs" Inherits="FileUpload.WebForm1" Src="upload.aspx.cs" %>


 <HTML>
         <HEAD>
                                                         <td>
                                                                 <asp:Label id="lblText2" runat="server" Font-Bold="True" Visible="false"> </asp:Label>
                                                         </td>
                                                 </tr>
                                                 <tr>
                                                         <td>
                                                                 <asp:Label id="lblText3" runat="server" Font-Bold="True" Visible="false"> </asp:Label>
                                                         </td>
                                                         <td>
                                                                 <asp:Label id="lblText4" runat="server" Font-Bold="True" Visible="false"> </asp:Label>
                                                         </td>
                                                 </tr>
                                                 <tr>
                                                         <td>
                                                                 <asp:Image id="imgFile" runat="server" Visible="False"></asp:Image>
                                                         </TD>
                                                         <td>
                                                         </td>
                                                 </TR>
                                 </TABLE>
                                 <hr>
                                 <p>Back to <a href="default.aspx/MyWiki.HomePage">HomePage</a></p>
                 </FORM>
         </body>
 </HTML>

Save this as "upload.aspx.cs" and put it in the main wiki directory, near the other files (like wikiedit.aspx).

 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;


 namespace FileUpload
 {
         /// <summary>
         /// Summary description for WebForm1.
         /// </summary>
         public class WebForm1 : System.Web.UI.Page
         {


                 protected Label lblFile;
                 protected HtmlInputFile filMyFile;
                 protected System.Web.UI.WebControls.Label lblInfo;
                 protected System.Web.UI.WebControls.Button cmdSend;
                 protected System.Web.UI.WebControls.Image imgFile;
                 protected System.Web.UI.WebControls.Label lblText1;
                 protected System.Web.UI.WebControls.Label lblText2;
                 protected System.Web.UI.WebControls.Label lblText3;
                 protected System.Web.UI.WebControls.Label lblText4;


                 override protected void OnInit(EventArgs e)
                 {
                         InitializeComponent();
                         base.OnInit(e);
                 }


                 private void InitializeComponent()
                 {    
                         this.Load += new System.EventHandler(this.Page_Load);
                         this.cmdSend.Click += new System.EventHandler(this.cmdSend_Click);
                 }


                 private void Page_Load(object sender, System.EventArgs e)
                 {


                 }


                 // Processes click on our cmdSend button
                 private void cmdSend_Click(object sender, System.EventArgs e)
                 {
                         // Check to see if file was uploaded
                         if( filMyFile.PostedFile != null )
                         {
                                 // 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;


                                         WriteToFile(strWriteToFile, ref myData);


                                         // Set label's text
                                         lblInfo.Text = 
                                                 "Filename: " + strFilename + "<br>" + 
                                                 "Size: " + nFileLen.ToString() + "<p>";




                                         // Set URL of the the object to point to the file we've just saved
                                         if (IsImageFile(strWriteToFile))
                                         {
                                                 lblText1.Text = "File available at:<br>";
                                                 lblText1.Visible = true;
                                                 imgFile.ImageUrl =  RootUrl(Request) + strDirectoryToWriteTo + "/" + OnlyFileName(strWriteToFile);
                                                 imgFile.ToolTip = "This file was stored as file.";
                                                 lblText3.Text = imgFile.ImageUrl;
                                                 // show the images and text
                                                 imgFile.Visible = true;
                                                 lblText3.Visible = true;
                                         }
                                         else
                                         {
                                                 lblText1.Text = "File available at:<br>";
                                                 lblText1.Visible = true;
                                                 lblText3.Text = RootUrl(Request) + strDirectoryToWriteTo + "/" + OnlyFileName(strWriteToFile);
                                                 lblText3.Visible = true;
                                         }


                                         lblInfo.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) || (strFullPath.IndexOf(".BMP") > 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)
                 {


                 string strReturnDirectory;
                 strReturnDirectory = "";
                 strFileName = strFileName.ToUpper();
                         if ((strFileName.EndsWith(".JPG"))||(strFileName.EndsWith(".GIF")) || (strFileName.EndsWith(".PNG")) || (strFileName.EndsWith(".BMP")))
                         {
                                 strReturnDirectory="images";
                         }
                         else if ((strFileName.EndsWith(".HTM"))||(strFileName.EndsWith(".HTML")))
                         {
                                 strReturnDirectory="html";
                         }
                         else if (strFileName.EndsWith(".DOC"))
                         {
                                 strReturnDirectory="doc";
                         }
                         else
                         {
                                 strReturnDirectory="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;
                 }
         }
 }

Now you still have to adapt the rights on the directory level, so that the ASPNET account is able to write to the directories "images" , "html", "doc", "files".

The upload script will have a default filesize limit of 4096 KB or less (see this Microsoft Knowledgebase article ) To change this, put the following parameter in the Web.config file:

Under the line:

                <compilation defaultLanguage="c#" debug="true" />

put the following line:

                <httpRuntime maxRequestLength="100000"/>

This will set the maximum upload limit to 100000 KB (100 MB).

Note

Back to HomePage

Not logged in. Log in

Welcome to the home of FlexWiki, a collaboration tool, based on WikiWiki, implemented using Microsoft .NET technologies

This is FlexWiki, an open source wiki engine.

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

Recent Topics