Solved

aspx.vb FileUploadControl.SaveAs issuess

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net application has warrnings about VB6 calls. 2 47
EF5 How do I stop pre-compiled views? 8 54
Modify Text File with Excel Macro 13 50
Subtract dates in vb.net 6 34
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…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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