.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

FileUpload path problem

Posted By:      Posted Date: September 26, 2010    Points: 0   Category :ASP.Net

I have not been able to uplaod a file to my web page although the code I am using works if I copy it locally. Perhaps I am missing something in the path or I need to change some security setting on the web page?

   protected void btnUpload_Click(object sender, EventArgs e)
        SelectedFileName.InnerHtml = txtFileField.PostedFile.FileName;
        FileType.InnerHtml = txtFileField.PostedFile.ContentType;

        // convert bytes to Kbytes
        int size = txtFileField.PostedFile.ContentLength / 1024;
        FileSize.InnerHtml = (size.ToString() + " KB");
        uploadDetails.Visible = true;

        string strFileName = txtFileField.PostedFile.FileName;
        string c = Path.GetFileName(strFileName);

            // not sure if this path is correct. not working anyway
            txtFileField.PostedFile.SaveAs("~/" + c); 
            feedBack.InnerHtml = "File uploaded successfully.";
        catch (Exception exp)
            string message = "File did not upload, please try again";
            feedBack.InnerHtml = message + "\n" + exp.Message.ToString();
            uploadDetails.Visible= false;
Thanks for any help.

View Complete Post

More Related Resource Links

Virtual Path Problem


 I get the following error on last line below (working on remote host): 'E:\web\sfcascom\ls_admin/uploads/images/001.JPG' is not a valid virtual path,

Note that the slashes go in different directions

I've tried a number of things but nothing seems to work. Thanks-


Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Web.UI.WebControls

Imports System.Drawing.Imaging.ImageCodecInfo
Partial Class Protected_imageupload
    Inherits System.Web.UI.Page
    Private Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        ' Initialize variables
        Dim sSavePath As String
        Dim sThumbExtension As String
        Dim intThumbWidth As Integer
        Dim intThumbHeight As Integer
        Dim sFilename As String = ""
        ' Set constant values
        'sSavePath = "images/"
        sSavePath = Server.MapPath("~/") + "ls_admin/uploads/images/"

        sThumbExtension = "_thumb"
        intThumbWidth = 160
        intThumbHeight = 120

        ' If file field isn?t empty
        If FilUpload.PostedFile IsNot Nothing Then
            ' Check file size (mustn?t be 0)
            Dim myFile As Http

FileUpload.SaveAs is configured to require a rooted path. but "http:// --- is not a rooted path


Hi Friends

when i am uploading image to http://filepath i got the 

The FileUpload.SaveAs Method is configured to require a rooted path but http:// filepath is not a rooted path

my code is as follows

If (Fileupload.HasFile)


Dim filepath As String = http://filepath/

FileUpload.SaveAs(filepath & String.Format("datafield_{0}_b.jpg", textbox.Text))

End If

Please help me




The Path problem?

I hope to get the following result, how can I write code? Thanks!                 Source                                                                                      Target1. http://localhost:1131/Mysite/Member/MyProject.aspx     =>       /Member/MyProject.aspx2. http://localhost:1131/Mysite/MyProject.aspx                   =>       /MyProject.aspx3. http://localhost:1131/Mysite/MyProject.aspx?Type=2     =>       /MyProject.aspx?Type=2 4. http://www.Mysite.com/Member/MyProject.aspx             =>       /Member/MyProject.aspx5. http://www.Mysite.com/MyProject.aspx   &nb

Problem in "Upload a picture into data base" because is in

Hi guys. I can upload a picture into database by this code correctly in a web page without "UpdatePanel": ******************************** SqlConnection connection = null; try { Byte[] imgByte = null; if (FileUpload.HasFile && FileUpload.PostedFile != null) { HttpPostedFile File = FileUpload.PostedFile; imgByte = new Byte[File.ContentLength]; File.InputStream.Read(imgByte, 0, File.ContentLength); } // Insert the employee name and image into db string conn = "Data Source=PC5;Initial Catalog=LaptopStore;Integrated Security=True"; connection = new SqlConnection(conn); connection.Open(); string sql = "INSERT INTO test(image) VALUES(@image) SELECT @@IDENTITY"; SqlCommand cmd = new SqlCommand(sql, connection); cmd.Parameters.AddWithValue("@image", imgByte); int id = Convert.ToInt32(cmd.ExecuteScalar()); lblResult.Text = String.Format("Employee ID is {0}", id); } catch { lblResult.Text = "There was an error"; } finally { connection.Close(); } ******************************************** But when I want to use this code in a

Problem in Path translation

Hi,I am developing a very basic application in C# and Asp.Net using New Website-->Asp.Net web site.This is a simple login control application available in Visual Studio.  After starting a project, I found folder structure like that Project name "WebSite2" under which I have  folders like "Account", "Scripts" and "Styles" and few files like "About.aspx", "Default.aspx" and "Web.config" under the same "WebSite2" folder. Under "Account" folder I have files like "ChangePassword.aspx","ChangePasswordSuccess.aspx","Login.aspx","Register.aspx" and "Web.config". Now I have add a simple page "ThankYou.aspx" under "Account" folder and "Thanks" under "WebSite2" folder. I have modified "Register.aspx.cs" file so that whenever a new user will register, an email will be sent to the persons inbox and from where he will activate his account and this functionality is working fine. Now the problem is that whenever a new user registered, an email will be send to him and simultaneously he will be landed to "ThankYou.aspx" page, but here the user is redirecting to "Login.aspx" page. i have tried it with so many combinations but always getting "Login.

Get Full Value of Fileupload path

Hi, I have a file upload control on my webform. When I want to use it to select a text file. I will not be uploaded the file however, I will need the value of the path so I can open the text file with the streamreader. I then will pass the value of the text file to a database. How do I get the value of the fileupload textbox?  Here is my code so far:   Dim FILENAME As String = NEED VALUE OF FILEUPLOAD TEXTBOX VALUE HERE Dim myfile As String Dim objStreamReader As StreamReader objStreamReader = File.OpenText(myfile.Text) Dim contents As String = objStreamReader.ReadToEnd() t.Text = contents objStreamReader.Close() 

file path problem


in the application, I need write a jpg file to local folder on server, then show it in browser.

so, the code likes show this file:

HttpContext.Current.Request.Url.Host + "/View/ViewJpg.aspx"

In the ViewJpg.aspx, it will process the jpg. /view is in root folder.

it works fine when the code is on a server, however, I have problem when I debug in on local machine.

because the code is actually http://localhost/mYproject but HttpContext.Current.Request.Url.Host only returns http://localhost.

what's the best way to deal with this?


FileUpload.SaveAs Problem


I'm uploading multiple PDF files from a CD to a folder on a server locally on the server itself. My problem is that the filenames and the PDF files is being uploaded, but the content of those PDF files themselves are missing. Their size is 0KB. 

FileUpload1.SaveAs(("C:\Documents and Settings\ABC\ABCD\ABCDE\PDF\" & Path.GetFileName((files(intCount1).ToString))
Not sure what i'm doing wrong here. I've tried a lot of other ways and I either get the path isn't found or or access is denied.
Thanks in advance for any help you guys can provide.




Fileupload problem



Can anybody help me out to clear the error. I am struggling from past few hours, my problem is this:

    private string SavePostedFiles(DevExpress.Web.ASPxUploadControl.UploadedFile uploadedFile)
        string ret = "";
        string fup;
        if (uploadedFile.IsValid)

get full path from fileupload control in client side???


<script type="text/javascript" >

        function SetA()

                    document.form1.action = 'Default.aspx?fileName='+document.getElementById('FileUpload1').value;
    <form id="form1" runat="server">
        <asp:FileUpload ID="FileUpload1" runat="server"  />
        <asp:Button ID="btnupload" runat="server" Text="Upload" OnClientClick="SetA()" />



here in the  javascript function i need send full path of upload control?


please help in this situation? if anybody knows other way of sending full path please tell?


thank you,

MVC Fileupload problem


hey guys I'm trying to write a simple file upload page using ASP MVC2.

I've read some blog post so I started like this

 foreach (string file in Request.Files)
                HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                if (hpf.ContentLength > 0)
                    string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads")

FileUpload Problem


Hi, I am having problems storing the path where an image is saved.

I will like to sotre the path: _images\DemoImg.jpg  and not C:\inetpub\wwwroot\sweb\_images\DemoImg.jpg

With path saved as C:\inetpub\wwwroot\sweb\_images\DemoImg.jpg I cant read the image unless I change the path in the database.

Any help or suggestions will be very appreciated.


Info Path Form Problem; the form is closed


Hi All

I am facing an error in info path form web enabled.


When i open a new form, it opens and saved. Even the sendingemail is also working.

But when i try to open the already saved form. The form is not opening it is displaying the error that the form has been closed. Or it is not getting the form from the location..

Problem in content deployment path job



I have created a Content Deployment path to copy data from source server to destination server.

And also i have created a job to run this content deployment path.

I made the job to run .But the job status is "PREPARING"  for a while.No change for a long time.

I dont know what might be the cause.

Could anyone help on this please?

problem in writing Element Path in Webservices has datsource for SSRS report



I have a requirement to create a webservice which returns dataset, once dataset is consumed by client application(here client app is SSRS report), I need to show dataset 's table value in SSRS rdl report.

When i see XML structure returned to client look like this

<?xml version="1.0" encoding="utf-8" ?>
<DataSet xmlns="http://tempuri.org/">
 <xs:schema id="Results" xmlns=""  
  <xs:element name="Results" msdata:IsDataSet="true"  
msdata:UseCurrentLocale="true" msprop:QueryTerms="Cool Bikes;"  
msprop:IgnoredNoiseWords="" msprop:Keyword="" msprop:ElapsedTime="938"  
msprop:Definition="" msprop:SpellingSuggestion="">
    <xs:choice minOccurs="0" maxOccurs="unbounded">
     <xs:element name="RelevantResults"

Path Too Long Problem. Accessing filenames longer than 260 chars using unicode and kernel32.dll.

I found this article a few weeks ago that provided a way to access files and folders past the 260 char length:


I put the code from "Finding Files and Folders" in a dll and wrote an app in vb accessing these functions.  However, the code randomly breaks on a specific kernel32 function call. 

I noticed that if the search finds a file (not folder) longer than 12 to 14 characters then it breaks on occasion.  This is strange because I would think it would either never break or always break.

Here's the line where it breaks:

// find next

found = FindNextFile(findHandle, out findData);

Any ideas?


fileupload problem


below the codes works. it save files to database whenever user click on it.

but what i want is user uploaded a file to database, and when he uploads again, the uploaded file will replace the previous file leaving the database only have a single file all times. instead of keep on making a new row to store files.

help to correct my code...thanks all.....

    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpload.Click
        If FileExtensionIsApproved(fUpload.HasFile) Then
            Dim test = fUpload.PostedFile.FileName
            Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ToString)
            Dim da As New SqlDataAdapter("Select * From files", con)
            Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
            Dim ds As New DataSet()
            da.MissingSchemaAction = MissingSchemaAction.AddWithKey
            Dim fs As New FileStream(test, FileMode.OpenOrCreate, FileAccess.Read)
            Dim MyData(fs.Length) As Byte
            fs.Read(MyData, 0, fs.Length)
            da.Fill(ds, "Mydata")
            Dim myRow As DataRow
            myRow = ds.Tables("Mydata").NewRow()
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend