none
File upload ASP.net -To share folder in different location or server RRS feed

  • Question

  • uploading image file to a share folder or mapped network drive, or other server  in other words, how to upload image file to a relative path location or mapped network drive instead of absolute path or rooted path. I am using follwing code but I am getting error like belo

     FileName = picUpload.FileName;
                if (Path.GetExtension(FileName).ToLower() != ".jpg")
                    Response.Write("Choose the correct format like.jpg ");
                if (picUpload.PostedFile.ContentLength > 131072)
                      Response.Write("The size of big too file , the size of the file must 128 KB not exceed!!! ");

                // string CurrentPath = Server.MapPath("~/upload/");
                string CurrentPath = Server.MapPath(@"\\Server\root\");
                 if (picUpload.HasFile)
                {

                    CurrentPath +=  FileName ;
                    picUpload.SaveAs(CurrentPath);
                    Label1.Text = " Received your file " + picUpload.FileName + " Content Type " + picUpload.PostedFile.ContentType + " Length " + picUpload.PostedFile.ContentLength;
                    
                }
                else
                {

                    Label1.Text = "No uploaded file";
                }
                


     

     

    Failed to map the path '/audcfs01/scs/'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: Failed to map the path '/audcfs01/scs/'.

    Source Error:

    The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:

    1. Add a "Debug=true" directive at the top of the file that generated the error. Example:

      <%@ Page Language="C#" Debug="true" %>

    or:

    2) Add the following section to the configuration file of your application:

    <configuration>
       <system.web>
           <compilation debug="true"/>
       </system.web>
    </configuration>

    Note that this second technique will cause all files within a given application to be compiled in debug mode. The first technique will cause only that particular file to be compiled in debug mode.

    Important: Running applications in debug mode does incur a memory/performance overhead. You should make sure that an application has debugging disabled before deploying into production scenario.


    Stack Trace:

    [InvalidOperationException: Failed to map the path '/audcfs01/scs/'.]
    System.Web.Hosting.HostingEnvironment.MapPathActual(VirtualPath virtualPath, Boolean permitNull) +8863322
    System.Web.Hosting.HostingEnvironment.MapPathInternal(VirtualPath virtualPath) +42
    System.Web.VirtualPath.MapPathInternal() +4
    System.Web.HttpRequest.MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, Boolean allowCrossAppMapping) +107
    System.Web.HttpRequest.MapPath(VirtualPath virtualPath) +37
    System.Web.HttpServerUtility.MapPath(String path) +99
    index.btnSubmit_Click(Object sender, EventArgs e) +210
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
    Thursday, May 5, 2011 6:38 PM

Answers

  • Thank you everyone for your helpful tips.

     

    I fixed this problem using the  code:

     private  void UploadPic(ref FileUpload  fuObj, ref Label fuLbl,  string fuName  )
        { 

     int fileSize = 5096000; string fileExt = ""; string pathAndFile = ""; Label1.Text = "";

    if (FileUpload1.PostedFile.ContentLength < fileSize)
                {
                  
                    fileExt = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                  
                    pathAndFile = "\\\\Server\\Folder\\";
                    pathAndFile += txtID.Text + "_" + txtLName.Text + "_" + FileUpload1.FileName.ToLower();
                    //FileName = pathAndFile;
                    Session["FileName"] = txtID.Text + "_" + txtLName.Text + "_" + FileUpload1.FileName.ToLower(); ;
                    if (fileExt == ".jpg")
                    {
                       
                            if (System.IO.File.Exists(pathAndFile))
                            {
                                Label1.Text = "Sorry Already Exists !!!";
                                return;
                            }
                            else
                            {
                                FileUpload1.SaveAs(pathAndFile);
                              
                            }
                       
                    }
                    else
                    {
                        Label1.Text = "Error Only .jpg Allowed";
                        return;
                    }
                  
                }
                else
                {
                    Label1.Text = "ERROR: The file is needs to be less than 4MB (4096 KB).";
                }

                Label1.Text = "Uploaded Successfully !! <br /> File Name :" +FileUpload1.FileName + "<br />  Content Type: " +
                                  FileUpload1.PostedFile.ContentType + "<br /> Length : " +FileUpload1.PostedFile.ContentLength;

    }


    Vejee
    • Marked as answer by vejees Tuesday, May 10, 2011 1:48 PM
    • Unmarked as answer by vejees Tuesday, May 10, 2011 8:48 PM
    • Marked as answer by Paul Zhou Wednesday, May 11, 2011 2:21 AM
    Tuesday, May 10, 2011 1:48 PM

All replies

  •  

    Hi VejeeMalli,

     

    I suggest a better forum for this question:

    Network Class Library

    There are experts in that forum.

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 10, 2011 9:33 AM
  • You can use Virtual directories. Create a virtual directory with IIS pointing to your network share. Then you can use a relative path of this virtual directory and not the actual path. One thing to note there will be permission issues occurs. You need to have IIS authenticated user who have privilege to read and write files. Have this same user name with same privilege in the network share machine as well. You may search MSDN or internet about virtual directory permissions for further information.
    Tuesday, May 10, 2011 11:24 AM
  • Thank you everyone for your helpful tips.

     

    I fixed this problem using the  code:

     private  void UploadPic(ref FileUpload  fuObj, ref Label fuLbl,  string fuName  )
        { 

     int fileSize = 5096000; string fileExt = ""; string pathAndFile = ""; Label1.Text = "";

    if (FileUpload1.PostedFile.ContentLength < fileSize)
                {
                  
                    fileExt = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                  
                    pathAndFile = "\\\\Server\\Folder\\";
                    pathAndFile += txtID.Text + "_" + txtLName.Text + "_" + FileUpload1.FileName.ToLower();
                    //FileName = pathAndFile;
                    Session["FileName"] = txtID.Text + "_" + txtLName.Text + "_" + FileUpload1.FileName.ToLower(); ;
                    if (fileExt == ".jpg")
                    {
                       
                            if (System.IO.File.Exists(pathAndFile))
                            {
                                Label1.Text = "Sorry Already Exists !!!";
                                return;
                            }
                            else
                            {
                                FileUpload1.SaveAs(pathAndFile);
                              
                            }
                       
                    }
                    else
                    {
                        Label1.Text = "Error Only .jpg Allowed";
                        return;
                    }
                  
                }
                else
                {
                    Label1.Text = "ERROR: The file is needs to be less than 4MB (4096 KB).";
                }

                Label1.Text = "Uploaded Successfully !! <br /> File Name :" +FileUpload1.FileName + "<br />  Content Type: " +
                                  FileUpload1.PostedFile.ContentType + "<br /> Length : " +FileUpload1.PostedFile.ContentLength;

    }


    Vejee
    • Marked as answer by vejees Tuesday, May 10, 2011 1:48 PM
    • Unmarked as answer by vejees Tuesday, May 10, 2011 8:48 PM
    • Marked as answer by Paul Zhou Wednesday, May 11, 2011 2:21 AM
    Tuesday, May 10, 2011 1:48 PM
  •  

    Hi,

     

    I'm glad to hear that you got it working.

    Thank you for sharing your solutions & experience here. It will be very beneficial for other community members who have similar questions.

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 11, 2011 2:20 AM
  • Hey I am trying to upload file to the shared hidden folder in network it gives me error 

    "The network name cannot be found" the path I am giving is 

    "Path = "\\\\qlnqdashweb01\\D\\apps\\DASHCRM\\DashFiles\\TypeAFiles"

    Can you please guide me what I should do or what is that am doing wrong

    Monday, November 27, 2017 4:08 PM
  • Hey I found the solution,

    So what I did was instead of using "file.SaveAs(Path.Combine(FPath, fname[0] + Path.GetExtension(file.FileName)));" that is file.save I used the following

    "System.IO.File.Copy(filename, FPath, true);" that is File.copy and the path which I gave is

     @"\\qlnqdashweb01\D$\apps\DASHCRM\DashFiles\TypeAFiles\Sathya.txt"

    Here we go then it worked for me hope it will help some one....

    Thank You

    Guys

    Tuesday, November 28, 2017 5:16 PM