Programalama > ASP.NET

<%@ Page Language="VB" debug=true  %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%  ''////////////////////////////////////////////////////////////////
    ''File uploading by Türkay Biliyor, turkay.biliyor@tekersan.com.tr
    ''///////////////////////////////////////////////////////////////
    Dim objConnection As OleDbConnection
    Dim objCommand As OleDbCommand
    Dim myReader As OleDbDataReader
    Dim strSQLQuery As String
     If Request.QueryString("mode") = "view" Then
        objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("Files.mdb") & ";")
        strSQLQuery = "SELECT * FROM Files WHERE ID =" & Request.QueryString("file_id") & " "
        objCommand = New OleDbCommand(strSQLQuery, objConnection)
        objConnection.Open()
        myReader = objCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
        If myReader.Read() Then
            Dim fileData() As Byte = CType(myReader.Item("FileData"), Byte())
            Response.Clear()
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + myReader.GetString(1))
            Response.ContentType = myReader.GetString(3)
            Response.OutputStream.Write(fileData, 0, fileData.Length)
        Else
            Response.Write("<p>No File to view.</p>")
        End If
        objConnection.Close()
    Else
%><html>
   <body>
     <script language="VB" runat="server">
       Sub btnUpload_OnClick(ByVal Src As Object, ByVal E As EventArgs)
           If Fileupload1.HasFile Then
               Dim files As HttpFileCollection = Request.Files
               Dim i As Integer = 0
               While i < Request.Files.Count
                   Dim file As HttpPostedFile = files(i)
                   If file.ContentLength > 0 Then
                       UploadFile(file)
                   End If
                   System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
               End While
               files = Nothing
           Else
               Response.Write("<p>Chose a file.</p>")
           End If
         End Sub
         
       Sub UploadFile(ByVal file As HttpPostedFile)
           Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("Files.mdb") & ";")
           Dim CmdText As String = "INSERT INTO Files(FileName, FileSize, ContentType, FileData) VALUES (@FileName, @FileSize, @ContentType, @FileData)"
           Dim fileName As String = Nothing
           Dim contentType As String = file.ContentType
           Dim fileLength As Integer = file.ContentLength
           Dim fileData(fileLength) As Byte
           Dim lastPos As Integer = file.FileName.LastIndexOf("\"c)
           fileName = file.FileName.Substring(System.Threading.Interlocked.Increment(lastPos))
           file.InputStream.Read(fileData, 0, fileLength)
             Try
                 Dim cmd As OleDbCommand = New OleDbCommand(CmdText, con)
                 Try
                     Dim pms As OleDbParameterCollection = cmd.Parameters
                     pms.Add("@FileName", OleDbType.VarChar, 50)
                     pms.Add("@FileSize", OleDbType.Integer)
                     pms.Add("@ContentType", OleDbType.VarChar, 50)
                     pms.Add("@FileData", OleDbType.VarBinary)
                     pms("@FileName").Value = fileName
                     pms("@FileSize").Value = fileLength
                     pms("@ContentType").Value = contentType
                     pms("@FileData").Value = fileData
                     pms = Nothing
                     con.Open()
                     cmd.ExecuteNonQuery()
                 Finally
                     CType(cmd, IDisposable).Dispose()
                 End Try
             Finally
                 CType(con, IDisposable).Dispose()
             End Try
       End Sub
   </script> 
<%
    objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("Files.mdb") & ";")
    If Request.QueryString("delete") <> "" Then
        strSQLQuery = "Delete from Files where ID = " & Request.QueryString("delete") & " "
        objCommand = New OleDbCommand(strSQLQuery, objConnection)
        objConnection.Open()
        objCommand.ExecuteNonQuery()
        objConnection.Close()
    End If
    strSQLQuery = "SELECT * FROM Files"
    objCommand = New OleDbCommand(strSQLQuery, objConnection)
    objConnection.Open()
    myReader = objCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    
%>
<table bgcolor="gainsboro" border="1">
    <form runat="server">
    <tr bgcolor="navy">
    <td colspan="4">
    <asp:button id="btn_Upload" runat="server" text="Upload" tabindex="2" OnClick = "btnUpload_OnClick"></asp:button> 
    <asp:fileupload id="Fileupload1" runat="server" />
    </td>
    </tr>
    </form>
    <% If myReader.HasRows Then%>
    <tr bgcolor="navy">
    <td><font color=white face=tahoma size=2>FileName</font></td>
    <td><font color=white face=tahoma size=2>FileSize</font></td>
    <td><font color=white face=tahoma size=2>ContentType</font></td>
    <td><font color=white face=tahoma size=2>Delete</font></td>
    </tr>
            <%While myReader.Read()%>
                <tr>
                <td><a href=main.aspx?file_id=<%=myReader.GetInt32(0)%>&mode=view><font  face=tahoma size=2><%=myReader.GetString(1)%></font></a> </td>
                <td><font  face=tahoma size=2><%=formatnumber(myReader.GetInt32(2).ToString()/1000,2)%> Kb</font></td>
                <td><font  face=tahoma size=2><%=myReader.GetString(3)%></font></td>
                <td><a href="main.aspx?delete=<%=myReader.GetInt32(0).ToString()%>"><font face=tahoma size=2 color=navy>Delete</font></a>
                </tr> 
            <%End While
            Else
                Response.Write("<p>No records.</p>")
            End If
            myReader.Close()
            objConnection.Close()  
    %>
</table> 
</body>
</html>
<%End if%>

türkay biliyor
turkay.biliyor@tekersan.com.tr


Yorumlar                 Yorum Yaz
Bu hazır kod'a ilk yorumu siz yapın!
KATEGORİLER
ASP - 240
ASP.NET - 24
C# - 75
C++ - 174
CGI - 8
DELPHI - 247
FLASH - 49
HTML - 536
PASCAL - 246
PERL - 11
PHP - 160
WML - 9
XML - 2
Copyright © 2002 - 2024 Hazır Kod - Tüm Hakları Saklıdır.
Siteden yararlanırken gizlilik ilkelerini okumanızı tavsiye ederiz.
hazirkod.com bir İSOBİL projesidir.