locked
FileUpload error - "overload resolution failed because no 'new' is accessible" RRS feed

  • Question

  • User1717218719 posted

    I have the following code and recieve the error "overload resolution failed because no 'new' is accessible" on the line

    Dim myFile As File = New File(contentType, originalName, fileData)

    any help with why this is the case andd how to fix would be great.

        Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) 'Handles btnUpload.Click
    
            Dim FileUpload1 As FileUpload = TryCast(FindControl("FileUpload1"), FileUpload)
            If FileUpload1.HasFile Then
                Dim contentType As String = FileUpload1.PostedFile.ContentType
                Dim fileData As Byte() = New Byte(FileUpload1.PostedFile.InputStream.Length) {}
                FileUpload1.PostedFile.InputStream.Read(fileData, 0, fileData.Length)
                Dim originalName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
                Dim myFile As File = New File(contentType, originalName, fileData)
                'Select Case AppConfiguration.DataStoreType
                '    Case DataStoreType.Database
                '        myFile.Save()
                '        Exit Select
                '    Case DataStoreType.FileSystem
                '        myFile.Save(Server.MapPath(Path.Combine(AppConfiguration.UploadsFolder, myFile.FileUrl)))
                '        Exit Select
                'End Select
                Response.Redirect("~/")
            End If
    
        End Sub

    Wednesday, January 29, 2020 10:33 AM

Answers

  • User1535942433 posted

    Hi E.RU,

    Accroding to your description,I guess you want to save files' content type,data and name into database.I suggest you could insert them using cmd.Parameters.AddWithValue.I create a demo for you.

    More details,you could refer to below codes:

    ASPX:

     <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click"/>
                <hr />
                <asp:Label ID="lblMessage" ForeColor="Green" runat="server" />

    Code-behind:

        Protected Sub btnUpload_Click(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(CType(fs.Length, Integer))
                    Dim constr As String = ConfigurationManager.ConnectionStrings("aspnet-TestApplicationWithDatabase-20190820030542").ConnectionString
                    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
    
            lblMessage.Text = "File uploaded successfully."
        End Sub

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 30, 2020 6:42 AM

All replies

  • Wednesday, January 29, 2020 11:29 AM
  • User753101303 posted

    Hi,

    Or maybe you try to use a 3rd party class that would include as well its own File class and for now there is some confusion with https://docs.microsoft.com/en-us/dotnet/api/system.io.file?view=netframework-4.8 because the correct namespace is not in scope ?

    Not sure form where you got this kind of code.

    Wednesday, January 29, 2020 11:54 AM
  • User1717218719 posted

    Thanks for your reply!

    I had a look at the links you provided and now understad that the File class is static so you cannot "New" a the class.

    However I am still strugling to get my code to do what I want.

    I am using a file upload and I want to get the content type, file data and original filename of the upload. once I have all of those I want to save It to my SQL database

    Wednesday, January 29, 2020 11:59 AM
  • User475983607 posted

    I had a look at the links you provided and now understad that the File class is static so you cannot "New" a the class.

    However I am still strugling to get my code to do what I want.

    I am using a file upload and I want to get the content type, file data and original filename of the upload. once I have all of those I want to save It to my SQL database

    What problem(s) are you trying to solve?  Are you having trouble getting the content type and file name?  Are you having trouble saving records in the database?  Are you receiving errors?  Have you made changes to the code?  

    Wednesday, January 29, 2020 12:17 PM
  • User1535942433 posted

    Hi E.RU,

    Accroding to your description,I guess you want to save files' content type,data and name into database.I suggest you could insert them using cmd.Parameters.AddWithValue.I create a demo for you.

    More details,you could refer to below codes:

    ASPX:

     <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click"/>
                <hr />
                <asp:Label ID="lblMessage" ForeColor="Green" runat="server" />

    Code-behind:

        Protected Sub btnUpload_Click(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(CType(fs.Length, Integer))
                    Dim constr As String = ConfigurationManager.ConnectionStrings("aspnet-TestApplicationWithDatabase-20190820030542").ConnectionString
                    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
    
            lblMessage.Text = "File uploaded successfully."
        End Sub

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 30, 2020 6:42 AM
  • User1717218719 posted

    Thank you for your reply. this solves my problem of uploading to database. However I still want to upload to my Azure storage conainer. I am unsure how to do this dispite having tried and searched many examples any help would be great.

    Here is code I tried which does not work. I dont recieve errors but nothing happens. Not sure if I am on the right track.

        Dim StorageConnStr As String = "(connection string)"
        Dim containerName As String = "test"
        Dim container As CloudBlobContainer
        Private gdvContainer As Object
    
        '--CREATE CONTAINER
        Private Async Function CreateContainer(ByVal cancelToken As CancellationToken) As Task
            Try
                Dim storAcc As CloudStorageAccount = CloudStorageAccount.Parse(StorageConnStr)
                Dim blobClient As CloudBlobClient = storAcc.CreateCloudBlobClient()
                container = blobClient.GetContainerReference(containerName)
                Await container.CreateIfNotExistsAsync(cancelToken)
    
                Dim blobContPermission As New BlobContainerPermissions()
                blobContPermission.PublicAccess = BlobContainerPublicAccessType.Container
    
                container.SetPermissions(blobContPermission)
            Catch ex As OperationCanceledException
                Throw New OperationCanceledException("Create container canceled")
            End Try
        End Function
    
        '--UPLOAD TO AZURE
        Public Async Function UploadAsync(ByVal path As String, ByVal cancelToken As CancellationToken) As Task
    
            Try
                Await CreateContainer(cancelToken)
    
                Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference(System.IO.Path.GetFileName(path))
    
                Await blockBlob.UploadFromFileAsync(path, cancelToken)
    
            Catch ex As OperationCanceledException
                Throw New OperationCanceledException("UploadAsync canceled")
            End Try
        End Function
    
    
        Protected Async Sub BtnSave_Click(sender As Object, e As EventArgs) 'Handles BtnSave.Click
    
            Dim StorageConnStr As String = "(Connection String)"
            Dim cancelToken As CancellationToken
            Dim appSettings As Object = ConfigurationManager.AppSettings
    
            Dim AZBlob As Object = (appSettings(StorageConnStr) & containerName)
            'Dim AZBlob As New AzureBlob(ConfigurationManager.AppSettings(StorageConnectionString), mycontainer)
            '-------
    
            Dim FileUpload1 As FileUpload = TryCast(FindControl("FileUpload1"), FileUpload)
            If FileUpload1.HasFile Then
                Dim contentType As String = FileUpload1.PostedFile.ContentType
                Dim fileData As Byte() = New Byte(FileUpload1.PostedFile.InputStream.Length) {}
                FileUpload1.PostedFile.InputStream.Read(fileData, 0, fileData.Length)
                Dim originalName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
                Dim myFile As File = New File(contentType, originalName, fileData)
    
                Await AZBlob.UploadAsync(originalName, cancelToken)
              
            End If
        End Sub

    Monday, February 3, 2020 9:53 AM