Solved

aspx.vb FileUploadControl.SaveAs issuess

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

632 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