Solved

file upload in asp.net using visual basic. Can I use a relative path instead of the actual?

Posted on 2008-10-22
6
767 Views
Last Modified: 2012-06-27
Hey Experts...

I'm trying to use a file upload in a CMS website. The below code works fine, but I'm wondering if there isn't a better/easier way to get it done.

I tried using: FileUpload1.SaveAs("~\files\"
I also tried using: FileUpload1.SaveAs("..\files\"

Neither of which worked. Is there a way to upload into that directory without using the actual folder name? I want to be able to move this to another folder later and want to use relative path if possible.
If FileUpload1.HasFile Then

            Try

                FileUpload1.SaveAs("C:\Web Sites\devBoyScouts\files\" & _

                   FileUpload1.FileName)

                Label1.Text = "File name: " & _

                   FileUpload1.PostedFile.FileName & "<br>" & _

                   "File Size: " & _

                   FileUpload1.PostedFile.ContentLength & " kb<br>" & _

                   "Content type: " & _

                   FileUpload1.PostedFile.ContentType

            Catch ex As Exception

                Label1.Text = "ERROR: " & ex.Message.ToString()

            End Try

Open in new window

0
Comment
Question by:MHenry
  • 3
  • 2
6 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 22783315
You can try
FileUpload1.SaveAs( Server.Mappath("\files\") & _
                   FileUpload1.FileName)
0
 
LVL 7

Author Comment

by:MHenry
ID: 22787980
pratima_mcs,

I'll give that a shot but I don't think it will work. The page is in the admin folder. The file folder is up one level so I need to do something like, ../files. Of course, that didn't work.

I would think...
0
 
LVL 3

Expert Comment

by:mickdoc
ID: 22797036
If I am not mistaken you MUST use an absolute path like "C:\blah blah".

I normally create a name value pair in the web.config file and just pull it from there.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 7

Author Comment

by:MHenry
ID: 22797401
mickdoc,

That's what I'm thinking too. Which really bites if you're developing in one location and moving it later. I'm just hoping there's some genius-type Expert who figured something out.

Thanks.
MH
0
 
LVL 3

Accepted Solution

by:
mickdoc earned 500 total points
ID: 22820393
I found this: http://forums.asp.net/p/1051511/1630574.aspx#1630574

I didn't think that worked (haven't tried it myself) but you can give it a go. It seemed to work for the guy that asked but I am sure I read somewhere that you must provide an exact path... hmmm, age isn't being kind to my poor brain I guess!
0
 
LVL 7

Author Closing Comment

by:MHenry
ID: 31509082
Hey mickdoc, that actually worked! Thanks for your help!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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