Forum Moderators: open

Message Too Old, No Replies

Managed File Upload File Editor

         

latoc

11:25 am on Jun 21, 2010 (gmt 0)

10+ Year Member



My website using aspx pages, requires a method for an authorised editor to login and upload files, say meeting minutes, in order that they may subsequently be viewed or printed. I am using Mysql database for the diary and guestbook pages. I have searched several forums but can’t seem to find any leads.
Can anyone help or point me in the right direction.
latoc

marcel

1:05 pm on Jun 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A simple way to do this is to use the asp:FileUpload control to save the file to the File System on the server, saving only the (relative) path to the file in your MySQL database.

hal12b

1:39 pm on Jun 21, 2010 (gmt 0)

10+ Year Member



Here's working code I am using on a page of mine.

<%@ Page Language="VB" %>
<%@ 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">

Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
If (upImage.HasFile) Then
If (CheckFileType(upImage.FileName)) Then
Dim filePath As String = "~/media-manager/" & upImage.FileName
upImage.SaveAs(MapPath(filePath))
End If
End If
End Sub

Function CheckFileType(ByVal fileName As String) As Boolean
Dim ext As String = Path.GetExtension(fileName)
Select Case ext.ToLower()
Case ".gif"
Return True
Case ".png"
Return True
Case ".jpg"
Return True
Case ".jpeg"
Return True
Case ".pdf"
Return True
Case ".doc"
Return True
Case ".xls"
Return True
Case ".docx"
Return True
Case ".xlsx"
Return True

Case Else
lblError.Text = "Invalid File Extension"
Return False
End Select
End Function

Sub Page_PreRender()
Dim upFolder As String = MapPath("~/media-manager/")
Dim dir As New DirectoryInfo(upFolder)
dlstImages.DataSource = dir.GetFiles()
dlstImages.DataBind()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>FileUpload File</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Label
id="lblImageFile"
Text="Image File:"
AssociatedControlID="upImage"
Runat="server" Font-Names="Verdana" Font-Size="10pt" />

<asp:FileUpload
id="upImage"
Runat="server" />

<br /><br />

<asp:Button
id="btnAdd"
Text="Upload File"
OnClick="btnAdd_Click"
Runat="server" />
<asp:Label ID="lblError" runat="server" ForeColor="Red" Font-Bold="True" Font-Names="Verdana" Font-Size="10pt"></asp:Label><br />
<br />


<hr />

<asp:DataList
id="dlstImages"
RepeatColumns="1"
runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Font-Names="Verdana" Font-Size="8pt" GridLines="Both" Width="94%">
<ItemTemplate>
Picture/file:<br /><asp:Image ID="Image1"
ImageUrl='<%# Eval("Name", "~/media-manager/{0}") %>'

Runat="server" />
<br /> <br />
link: [yourwebsite...] Eval("Name") %>
</ItemTemplate>


<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedItemStyle BackColor="#738A9C" BorderColor="White" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:DataList>

</div>
</form>
</body>
</html>

latoc

4:29 pm on Jun 21, 2010 (gmt 0)

10+ Year Member



Thank you both for your response and help.I had provisionally tried the fileUpload control but considered it not appropriate,however I will revisit.
Thank you hal12b for the complete code which is very helpful. I will work on this issue and report progress.
latoc