Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

aspx.vb FileUploadControl.SaveAs issuess

Posted on 2014-04-21
2
Medium Priority
?
301 Views
Last Modified: 2014-04-21
I'm working through my first attempt at using aspx.vb in the back ground using the fileupload paramter in my default.aspx page.  Although I can select the file to upload, each time I try to upload and save the file into a newly created folder, using an alternative file name nothing happens. Where is my code incorrect?

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Linq
Imports System.Web
Imports System.Web.UI.Page
Imports System.Web.UI.WebControls
Imports System.Management.Automation
Imports System.Management.Automation.Runspaces
Imports System.IO
Imports System.Text

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs)

        'Dim fsoied, ftxt
        'Dim fstatus As String
        Dim fileOK As Boolean = False
        'Dim HasfileOK As Boolean = False

        If FileUploadControl.HasFile Then
                Dim fileExtension As String
                fileExtension = System.IO.Path. _
                    GetExtension(FileUploadControl.FileName).ToLower()
                Dim allowedExtensions As String() = _
                    {".txt"}
                For i As Integer = 0 To allowedExtensions.Length - 1

                'Dim Filesize As Integer = .PostedFileContent
                If fileExtension = allowedExtensions(i) Then
                        fileOK = True
                    End If
        Next

            If fileOK Then
                Try
                    Dim objFSO, objFolder
                    objFSO = CreateObject("Scripting.FileSystemObject")
                    'strFolder = objFSO.CreateFolder(Server.MapPath("D:\Reports\uploads\" & TextBox1.Text))
                    objFolder = "D:\Reports\uploads\" & TextBox1.Text & "-" & DateTime.Now.ToString("yyyyMMdd-HHmmss.fff")
                    If objFSO.FolderExists(objFolder) = True Then
                        StatusLabel.Text = "Upload status: File was not uploaded, as import folder is identical"
                    Else
                        objFSO.CreateFolder(objFolder)
                        StatusLabel.Text = "Upload status: Creating Upload folder " & objFolder & ", Please now wait"
                        Dim virtualFolder As String = "~/objFolder/"
                        Dim physicalFolder As String = Server.MapPath(virtualFolder)
                        Dim fileName As String = Guid.NewGuid().ToString
                        Dim extension As String = System.IO.Path.GetExtension(FileUploadControl.FileName)
                        FileUploadControl.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension))
                        StatusLabel.Text = "Upload status: File uploaded!"
                    End If

                Catch ex As Exception
                    StatusLabel.Text = "Upload status: Only text files are under 100kb are accepted"

                End Try
            Else
                StatusLabel.Text = "Upload status: Cannot Accept File"

            End If
        End If
    End Sub

End Class

Open in new window

0
Comment
Question by:patelbg2001
  • 2
2 Comments
 
LVL 6

Author Comment

by:patelbg2001
ID: 40013084
This is why i cannot save to my newly created directory -

Dim virtualFolder As String = "~/objFolder/"

Open in new window


if I set the virtual folder to this, the file upload works fine

Dim virtualFolder As String = "~/"

Open in new window


If I've created a new folder, as part of the process, how can i store the folder path created to use within the aspx.vb later (as the folder is time stampted)?

 
objFSO = CreateObject("Scripting.FileSystemObject") 
objFolder = "D:\Reports\uploads\" & TextBox1.Text & "-" & DateTime.Now.ToString("yyyyMMdd-HHmmss.fff")
objFSO.CreateFolder(objFolder)

Open in new window

0
 
LVL 6

Accepted Solution

by:
patelbg2001 earned 0 total points
ID: 40013479
Resolved

Dim physicalFolder As String = objFolder.ToString

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question