Upload File and Save in Folder C#

RRS feed

  • Remove From My Forums

 locked

how to upload file into folder inside virtual directory RRS feed

  • Question

  • User-1634604574 posted

    i am used this code simply it is not save file into folder within virtual directory

                      public void Upload3()         {                            string savePath = ConfigurationManager.AppSettings["C:\temp"] ;              for (int i = 0; i < Request.Files.Count; i++)             {                 var file = Request.Files[i];                  var fileName = Path.GetFileName(file.FileName);                  var path = Path.Combine(savePath, fileName);                 file.SaveAs(path);             }         }

All replies

  • User475983607 posted

    Rather than telling u.s.a. what is not happening draw what actually happens.

    Endeavor standard debugging.  Sett a break betoken and step through your code.    What is the value of savePath?  Does the code ever enter the for...loop?  what's the value of file.FileName?  These are basic troubleshooting pace that you should perform since yous have the lawmaking.

  • User-1634604574 posted

    i want to salvage file into folder within local my problem is i want to this line code dynamically

    inside spider web.config

    <add cardinal="DocumentationLocation" value="C:\temp" />

    i wrote

     this lawmaking

                        [HttpPost]         public ActionResult JqAJAX(cord parametter)         {                        try             {                 Configuration config = System.Spider web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");                 config.AppSettings.Settings.Remove("DocumentationLocation");                 config.AppSettings.Settings.Add together("DocumentationLocation2", parametter);                 config.Salve();                  render Json(new                 {                     msg = "Successfully added"                 });             }             take hold of (Exception ex)             {                 throw ex;             }         }                  

    i desire to brand this value

    <add key="DocumentationLocation" value="C:\temp" /> dynamically

  • User475983607 posted

    I do not empathise what you are trying to exercise.

    Store the virtual folder in appSettings.

    <add key="DocumentationLocation" value="~/temp" />

    And so fetch the setting and using information technology to get the absolute path.

    stirng virtualFolder = ConfigurationManager.AppSettings["DocumentationLocation"]; string path = Server.MapPath(virtualFolder);

    If the value is dynamic then shop the value in a database table.

  • User-1634604574 posted

    i don't want to use

                        Server                    .                    MapPath because i desire to save file exterior from binder application i saved it on virtual directory on iis                  
  • User475983607 posted

    i don't want to use Server.MapPath because i desire to save file outside from folder awarding i saved it on virtual directory on iis

    Your requirement is how to salve a file path exterior the current virtual directory.   You lot are the only person that knows the path to this unknown directory.

    But y'all tin certainly store the file path in appSettings.

    <add together primal="DocumentationLocation" value="C:\temp" />
    string path = ConfigurationManager.AppSettings["DocumentationLocation"];

     If the path is dynamic then you are nevertheless the simply person that knows the path requirements.   I assume a dynamic path requires a data driven approach that requires a bit of pattern piece of work on your side.

  • User-1634604574 posted

    every fourth dimension you are not understand on my question i don't know why..

  • User475983607 posted

    Your clarification and code are contradictory.  First yous y'all want upload a file in to local which I presume mean local to the application where the code is running.

    i am used this code but information technology is non save file into binder inside virtual directory inside web.config

    i want to save file into folder inside local my problem is i want to this line lawmaking dynamically

    Later on yous desire to save the file outside the current web application and the new code does non upload a file but looks like you are trying to update the web.config.

    Server.MapPath because i desire to save file outside from folder application i saved it on virtual directory on iis

    What are you trying to practise exactly?  If you are trying to update the web.config then y'all need continue in mind that doing and so causes the web application to restart.   This is mostly unwanted behavior and not dynamic, information technology'southward static.

    If the file path is dynamic and then where is the dynamic data coming from?

  • User-1634604574 posted

    i just want to save file in binder on  local c and name of that binder is dynamic non static

  • User753101303 posted

    Howdy,

    And you'll compute this folder from what? Usually you configure a root path once for all and then you possibly create sub folders if needed.

    Changinge the spider web.config would restart the web site and how you would recall previously saved files ? Your intent is unclear to me as well.

  • User475983607 posted

    i just want to save file in folder on  local c and proper noun of that folder is dynamic not static

    Create a root folder on the machine.  Grant the application pool identity read/write admission to the root folder.  https://docs.microsoft.com/en-u.s.a./iis/manage/configuring-security/application-pool-identities

    The dynamic directory paths inside the root folder can be whatever you like.  Y'all can grant the application pool access to a bulldoze like the C drive but that's not secure.

  • User-1634604574 posted

    i have folder on local C name of that folder is temp

    i want to save image file or any other type of file into that folder that folder is outside of my project information technology mean we cannot use server.map to upload files into that binder

  • User753101303 posted

    Ok so it could be :

    public void Upload3() {    // Is savePath the expected value?    string savePath = ConfigurationManager.AppSettings["<actual key for your settings>"] ;    // Append a subfolder if needed (create it if needed, permissions needed)    savePath=Organization.IO.Path.Combine(savePath, <any provides a sub binder name>);    for (int i = 0; i < Request.Files.Count; i++)    {       var file = Request.Files[i];       var fileName = Path.GetFileName(file.FileName);       var path = Path.Combine(savePath, fileName);       file.SaveAs(path);    } }

    I wonder know if the trouble was reading the folder name from your web.config file (as you tried c:\temp starting time and then attempted ~) ?

    It'due south not ever easy to guess from the code (is the  key named this mode, is this a typo when copying the lawmaking) or even to spot an error at some place where in your own experience, you don't expect an fault.

    That'due south why every bit suggested already, you should always tell which mistake message or unexpected beliefs you accept when posting code that "doesn't work".

  • User-1634604574 posted

    at present i want to make proper noun of this sub folder dynamic i wrote this but is not truthful i don't know why

    controller

                        [HttpPost]         public void Upload4(string p)         {             // Is savePath the expected value?             string savePath = ConfigurationManager.AppSettings["DocumentationLocation"];             // Append a subfolder if needed (create it if needed, permissions needed)             savePath = System.IO.Path.Combine(savePath, p);             for (int i = 0; i < Request.Files.Count; i++)             {                 var file = Request.Files[i];                 var fileName = Path.GetFileName(file.FileName);                 var path = Path.Combine(savePath, fileName);                 file.SaveAs(path);             }         }

    view

                        var formData = new FormData();                 var totalFiles = certificate.getElementById("getFile").files.length;                  for (var i = 0; i < totalFiles; i++) {                     var file = document.getElementById("getFile").files[i];                      formData.append("getFile", file);                 }                  var req={p:'a',formData}                 $.ajax({                     type: "POST",                     url: '/Letter_Head/Upload4',                     data: JSON.stringify(req),                     dataType: 'json',                     contentType: faux,                     processData: simulated,                     success: function (response) {                         alert('succes!!');                     },                      fault: office (fault) {                     }                 })
  • User475983607 posted

    now i desire to make proper name of this sub folder dynamic i wrote this but is not true i don't know why

    Yous have total control over the folder names.  Information technology is not articulate why you are having trouble creating a folder proper name.  Perchance yous are bold yous can get the folder path course the file upload?  Again, information technology would exist helpful if you clearly explain your expectation rather than making the community judge.

    Lastly, the AJAX function is incorrect.   The docs illustrate how to transport FormData.

    https://developer.mozilla.org/en-US/docs/Spider web/API/FormData/Using_FormData_Objects

faustimpinty57.blogspot.com

Source: https://social.msdn.microsoft.com/Forums/en-US/3d3c08a6-79a4-4a22-b210-a53f6d7ba8c6/how-to-upload-file-into-folder-inside-virtual-directory

0 Response to "Upload File and Save in Folder C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel