Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

aspx.vb FileUploadControl.SaveAs issuess

Posted on 2014-04-21
2
Medium Priority
?
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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