Solved

aspx.vb FileUploadControl.SaveAs issuess

Posted on 2014-04-21
2
292 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now