locked
how to send parameter to method void RRS feed

  • Question

  • User-1634604574 posted

    i have this code to send parameter from view to this method

      [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

      $("#send").click(function () {
    
                var formData = new FormData();
                var totalFiles = document.getElementById("file").files.length;
    
                for (var i = 0; i < totalFiles; i++) {
                    var file = document.getElementById("file").files[i];
    
                    formData.append("file", file);
                }
    
                 
    
                $.ajax({
                    type: "POST",
                    url: '/Letter_Head/Upload4',
                    data: { p: "a" },
                    dataType: 'json',
                    contentType: false,
                    processData: false,
                    success: function (response) {
                        alert('succes!!');
                    },
    
                    error: function (error) {
                    }
                }).done(function () {
    
                    $.ajax({
                        type: "POST",
                        url: '/Letter_Head/Upload4',
                        data: formData,
                        dataType: 'json',
                        contentType: false,
                        processData: false,
                        success: function (response) {
                            alert('succes!!');
                        },
    
                        error: function (error) {
                        }
                    })
                })
            })

    but the parameter doesn't send to that method upload4 why?

    Saturday, May 23, 2020 6:27 AM

Answers

  • User475983607 posted

    but the parameter doesn't send to that method upload4 why?

    There are several logical issue with your code.  IMHO, the biggest issue is it seems you you're not using debugging tools.  The code tries to send two request and it should be very clear that the action is hit twice.  Anyway, I crafted a working and verified demo based on your code.  Feel free to update the code to your needs.

    public class FileUploadController : Controller
        {
            // GET: FileUpload
            public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Upload(string p)
            {
                Dictionary<string, string> savedFiles = new Dictionary<string, string>();
                // 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);
                    savedFiles.Add(i.ToString(), path);
                }
                return Json(savedFiles);
            }
        }
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    <form method="post">
        <div>
            <input id="files" type="file" name="files" multiple />
        </div>
        <div>
            <input id="path" type="text" name="path" value="Documentation"/>
        </div>
        <div>
            <input id="send" type="submit" value="submit" />
        </div>
        <div id="results"></div>
    </form>
    
    @section scripts{
     <script>
         $("#send").click(function (e) {
             e.preventDefault();
    
             var formData = new FormData();
             var totalFiles = document.getElementById("files").files.length;
    
             for (var i = 0; i < totalFiles; i++) {
                 var file = document.getElementById("files").files[i];
                 formData.append("files", file, file.name);
             }
             var path = document.getElementById("path").value;
             formData.append("p", path);
    
             $.ajax({
                 type: "POST",
                 url: '@Url.Action("Upload")',
                 data: formData,
                 dataType: 'json',
                 contentType: false,
                 processData: false,
                 success: function (response) {
                     console.log(response);
                     for (const [key, value] of Object.entries(response)) {
                         $('#results').append("<div>", key + ': ' + value);
                     }
                 },
                 error: function (error) {
                     console.log(error);
                 }
             });
         });
     </script>   
     }
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 23, 2020 11:51 AM
  • User475983607 posted

    i want to save file inside folder a which is that folder a was inside folder temp and that folder temp is inside local C i want to send folder a name as a parameter

    to upload file inside it so that code should be ran on iis because folder temp is a virtual directory on iis and ran your sample it doesn't upload my file on folder a

    which is that folder a is a sub-folder inside temp folder 

    This is a new requirement.   As stated above, you can update the code to suite your needs.  All you have you have is create the folder if the folder does not exist.  Also you must grant write capabilities to the "Temp" folder.  Keep in mind, this process was previously discussed in one of your other threads.

    Anyway, I made a small change to the original code to create folder if the folder does not exist.  I verified the code works as expected and writes to C:\Temp\[folder].

    [HttpPost]
    public ActionResult Upload(string p)
    {
        Dictionary<string, string> savedFiles = new Dictionary<string, string>();
    
        string savePath = ConfigurationManager.AppSettings["DocumentationLocation"];
        savePath = System.IO.Path.Combine(savePath, p);
    
        if (!Directory.Exists(savePath))
        {
            Directory.CreateDirectory(savePath);
        }
    
        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);
            savedFiles.Add(i.ToString(), path);
        }
        return Json(savedFiles);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 23, 2020 2:12 PM

All replies

  • User475983607 posted

    but the parameter doesn't send to that method upload4 why?

    There are several logical issue with your code.  IMHO, the biggest issue is it seems you you're not using debugging tools.  The code tries to send two request and it should be very clear that the action is hit twice.  Anyway, I crafted a working and verified demo based on your code.  Feel free to update the code to your needs.

    public class FileUploadController : Controller
        {
            // GET: FileUpload
            public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Upload(string p)
            {
                Dictionary<string, string> savedFiles = new Dictionary<string, string>();
                // 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);
                    savedFiles.Add(i.ToString(), path);
                }
                return Json(savedFiles);
            }
        }
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    <form method="post">
        <div>
            <input id="files" type="file" name="files" multiple />
        </div>
        <div>
            <input id="path" type="text" name="path" value="Documentation"/>
        </div>
        <div>
            <input id="send" type="submit" value="submit" />
        </div>
        <div id="results"></div>
    </form>
    
    @section scripts{
     <script>
         $("#send").click(function (e) {
             e.preventDefault();
    
             var formData = new FormData();
             var totalFiles = document.getElementById("files").files.length;
    
             for (var i = 0; i < totalFiles; i++) {
                 var file = document.getElementById("files").files[i];
                 formData.append("files", file, file.name);
             }
             var path = document.getElementById("path").value;
             formData.append("p", path);
    
             $.ajax({
                 type: "POST",
                 url: '@Url.Action("Upload")',
                 data: formData,
                 dataType: 'json',
                 contentType: false,
                 processData: false,
                 success: function (response) {
                     console.log(response);
                     for (const [key, value] of Object.entries(response)) {
                         $('#results').append("<div>", key + ': ' + value);
                     }
                 },
                 error: function (error) {
                     console.log(error);
                 }
             });
         });
     </script>   
     }
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 23, 2020 11:51 AM
  • User-1634604574 posted

    is that code when you are wrote work on your side?

    Saturday, May 23, 2020 12:06 PM
  • User475983607 posted

    is that code when you are wrote work on your side?

    Yes.  As stated in my first post.  The demo code was tested and verified.  

    Saturday, May 23, 2020 12:17 PM
  • User-1634604574 posted

    i want to save file inside folder a which is that folder a was inside folder temp and that folder temp is inside local C i want to send folder a name as a parameter

    to upload file inside it so that code should be ran on iis because folder temp is a virtual directory on iis and ran your sample it doesn't upload my file on folder a

    which is that folder a is a sub-folder inside temp folder 

    Saturday, May 23, 2020 1:16 PM
  • User475983607 posted

    i want to save file inside folder a which is that folder a was inside folder temp and that folder temp is inside local C i want to send folder a name as a parameter

    to upload file inside it so that code should be ran on iis because folder temp is a virtual directory on iis and ran your sample it doesn't upload my file on folder a

    which is that folder a is a sub-folder inside temp folder 

    This is a new requirement.   As stated above, you can update the code to suite your needs.  All you have you have is create the folder if the folder does not exist.  Also you must grant write capabilities to the "Temp" folder.  Keep in mind, this process was previously discussed in one of your other threads.

    Anyway, I made a small change to the original code to create folder if the folder does not exist.  I verified the code works as expected and writes to C:\Temp\[folder].

    [HttpPost]
    public ActionResult Upload(string p)
    {
        Dictionary<string, string> savedFiles = new Dictionary<string, string>();
    
        string savePath = ConfigurationManager.AppSettings["DocumentationLocation"];
        savePath = System.IO.Path.Combine(savePath, p);
    
        if (!Directory.Exists(savePath))
        {
            Directory.CreateDirectory(savePath);
        }
    
        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);
            savedFiles.Add(i.ToString(), path);
        }
        return Json(savedFiles);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 23, 2020 2:12 PM