How to Upload File From Directory in C#
- Remove From My Forums
-
Question
-
User-1634604574 posted
i am used this code but it is not relieve file into folder inside 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 us what is not happening describe what really happens.
Endeavor standard debugging. Sett a break signal and step through your lawmaking. What is the value of savePath? Does the code always enter the for...loop? what'due south the value of file.FileName? These are basic troubleshooting step that you should perform since yous accept the code.
-
User-1634604574 posted
i desire to save file into folder within local my problem is i want to this line code dynamically
inside spider web.config
<add central="DocumentationLocation" value="C:\temp" />
i wrote
this code
[HttpPost] public ActionResult JqAJAX(cord parametter) { effort { Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); config.AppSettings.Settings.Remove("DocumentationLocation"); config.AppSettings.Settings.Add together("DocumentationLocation2", parametter); config.Save(); return Json(new { msg = "Successfully added" }); } grab (Exception ex) { throw ex; } }i want to make this value
<add primal="DocumentationLocation" value="C:\temp" /> dynamically
-
User475983607 posted
I do not understand what you are trying to practice.
Store the virtual folder in appSettings.
<add together cardinal="DocumentationLocation" value="~/temp" />
Then fetch the setting and using information technology to go the accented path.
stirng virtualFolder = ConfigurationManager.AppSettings["DocumentationLocation"]; string path = Server.MapPath(virtualFolder);
If the value is dynamic and then store the value in a database tabular array.
-
User-1634604574 posted
i don't want to utilize
Server . MapPath considering i desire to save file outside from folder application i saved it on virtual directory on iis
-
User475983607 posted
i don't want to use Server.MapPath because i want to save file outside from folder application i saved it on virtual directory on iis
Your requirement is how to save a file path outside the current virtual directory. You are the only person that knows the path to this unknown directory.
But you tin certainly store the file path in appSettings.
<add together fundamental="DocumentationLocation" value="C:\temp" />
string path = ConfigurationManager.AppSettings["DocumentationLocation"];
If the path is dynamic then you are still the but person that knows the path requirements. I assume a dynamic path requires a data driven approach that requires a flake of design work on your side.
-
User-1634604574 posted
every time you are not empathise on my question i don't know why..
-
User475983607 posted
Your clarification and lawmaking are contradictory. First you lot you want upload a file in to local which I assume mean local to the application where the code is running.
i am used this code only it is not 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 code dynamically
Later on yous want to salvage the file outside the current web application and the new code does not upload a file but looks like you are trying to update the spider web.config.
Server.MapPath considering i want to relieve file exterior from binder application i saved it on virtual directory on iis
What are you lot trying to practise exactly? If you are trying to update the web.config then you demand keep in mind that doing so causes the web awarding to restart. This is by and large unwanted behavior and not dynamic, it's static.
If the file path is dynamic then where is the dynamic data coming from?
-
User-1634604574 posted
i but want to save file in folder on local c and name of that folder is dynamic non static
-
User753101303 posted
Hi,
And you lot'll compute this folder from what? Ordinarily you configure a root path once for all and then you possibly create sub folders if needed.
Changinge the web.config would restart the spider web site and how you would retrieve previously saved files ? Your intent is unclear to me as well.
-
User475983607 posted
i merely desire to save file in folder on local c and name of that folder is dynamic not static
Create a root binder on the automobile. Grant the awarding puddle identity read/write access to the root folder. https://docs.microsoft.com/en-u.s.a./iis/manage/configuring-security/awarding-pool-identities
The dynamic directory paths within the root binder tin exist whatever yous like. You can grant the awarding pool admission 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 prototype file or any other blazon of file into that folder that binder is outside of my project it mean nosotros cannot use server.map to upload files into that folder
-
User753101303 posted
Ok so it could be :
public void Upload3() { // Is savePath the expected value? cord savePath = ConfigurationManager.AppSettings["<actual key for your settings>"] ; // Append a subfolder if needed (create it if needed, permissions needed) savePath=System.IO.Path.Combine(savePath, <any provides a sub folder name>); for (int i = 0; i < Request.Files.Count; i++) { var file = Asking.Files[i]; var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(savePath, fileName); file.SaveAs(path); } }I wonder know if the problem was reading the folder name from your web.config file (every bit you tried c:\temp first and so attempted ~) ?
It's not always easy to approximate from the code (is the key named this way, is this a typo when copying the code) or even to spot an mistake at some place where in your own experience, you don't wait an error.
That'south why as suggested already, you should e'er tell which fault bulletin or unexpected behavior you accept when posting lawmaking that "doesn't work".
-
User-1634604574 posted
now i want to make proper name of this sub folder dynamic i wrote this only is not true 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 < Asking.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 = certificate.getElementById("getFile").files[i]; formData.suspend("getFile", file); } var req={p:'a',formData} $.ajax({ blazon: "POST", url: '/Letter_Head/Upload4', information: JSON.stringify(req), dataType: 'json', contentType: faux, processData: false, success: function (response) { alarm('succes!!'); }, error: function (error) { } }) -
User475983607 posted
now i want to make name of this sub folder dynamic i wrote this but is not truthful i don't know why
You take total control over the folder names. Information technology is non clear why you are having problem creating a folder name. Perhaps you are assuming you can get the folder path form the file upload? Again, it would be helpful if you conspicuously explain your expectation rather than making the community guess.
Lastly, the AJAX office is incorrect. The docs illustrate how to transport FormData.
https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
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 "How to Upload File From Directory in C#"
Publicar un comentario