question

Martha-9373 avatar image
0 Votes"
Martha-9373 asked DuaneArnold-0443 commented

Upload entire folder file upload VB.NET

Hi all,

I have code that uploads files to SQL database and displays them in a gridview. In the gridview you can download the file.
I would like to display these files in a folder in the SQL database (if possible) and in folders on my UI (gridview)

I have the below code which uploads, displays and downloads.
does anybody have any idea how to create the folder aspect I am looking for ?
Note: I don't want to use any cloud platforms / applications

Thanks in advance :)

    Protected Sub Upload(sender As Object, e As EventArgs)
         Dim filename As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
         Dim contentType As String = FileUpload1.PostedFile.ContentType
         Using fs As Stream = FileUpload1.PostedFile.InputStream
             Using br As New BinaryReader(fs)
                 Dim bytes As Byte() = br.ReadBytes(DirectCast(fs.Length, Long))
                 Using con As New SqlConnection(constr)
                     Dim query As String = "insert into tblFiles values (@Name, @ContentType, @Data)"
                     Using cmd As New SqlCommand(query)
                         cmd.Connection = con
                         cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename
                         cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contentType
                         cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes
                         con.Open()
                         cmd.ExecuteNonQuery()
                         con.Close()
                     End Using
                 End Using
             End Using
         End Using
         Response.Redirect(Request.Url.AbsoluteUri)
     End Sub
    
     Private Sub BindGrid()
         Using con As New SqlConnection(constr)
             Using cmd As New SqlCommand()
                 cmd.CommandText = "select * from table1"
                 cmd.Connection = con
                 con.Open()
                 GridView1.DataSource = cmd.ExecuteReader()
                 GridView1.DataBind()
                 con.Close()
             End Using
         End Using
     End Sub
    
     Protected Sub DownloadFile(sender As Object, e As EventArgs)
         Dim id As Integer = Integer.Parse(TryCast(sender, LinkButton).CommandArgument)
         Dim bytes As Byte()
         Dim fileName As String, contentType As String
         Using con As New SqlConnection(constr)
             Using cmd As New SqlCommand()
                 cmd.CommandText = "SELECT Name, Data, ContentType from tblFiles where id=@id"
                 cmd.Parameters.AddWithValue("@id", id)
                 cmd.Connection = con
                 con.Open()
                 Using sdr As SqlDataReader = cmd.ExecuteReader()
                     sdr.Read()
                     bytes = DirectCast(sdr("Data"), Byte())
                     contentType = sdr("ContentType").ToString()
                     fileName = sdr("Name").ToString()
                 End Using
                 con.Close()
             End Using
         End Using
         Response.Clear()
         Response.Buffer = True
         Response.Charset = ""
         Response.Cache.SetCacheability(HttpCacheability.NoCache)
         Response.ContentType = contentType
         Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName)
         Response.BinaryWrite(bytes)
         Response.Flush()
         Response.End()
     End Sub
dotnet-visual-basic
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MS SQL Server doesn't have folders. You can only create a folder possibly in your case to hold a file on a the computer's harddrive that the ASP.NET Worker Process wpw3.exe that is hosting the ASP.NET Web program has permissions to access the resource.

0 Votes 0 ·

Thanks for your reply! Do you have any examples of how I could display the folders on my ASP.NET User interface as I ​am completely unfamiliar with wpw3.ex

0 Votes 0 ·

1 Answer

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered DuaneArnold-0443 commented

@Martha-9373

The IIS or aka ASP.NET Worker Process..

https://stackify.com/w3wp-exe-iis-worker-process/

As far has getting a list of the folders and displaying them, the link may help you.

https://www.aspsnippets.com/Articles/Display-list-of-files-from-Server-folder-in-ASPNet-GridView.aspx

The w3wp.exe that is hosting your program and whatever account it uses must have the appropriate permissions to access a given resource, like a directory.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi DuaneArnold-0443,

I have code that uploads and downloads the files but unfortunately does not display in files in folders.
I do not have permission to use w3wp.exe.

Thanks

0 Votes 0 ·

I don't see why you can't use the code that was given to you to display files in a folder. You may have to tweak the code for your needs, but I don't see why the code cannot help you.

And you do have control as to what credentials w3wp.exe presents to resources on the behalf of your ASP.NET program, which is called ASP.NET Impersonation.

https://docs.microsoft.com/en-us/troubleshoot/aspnet/implement-impersonation

0 Votes 0 ·