locked
How can I copy file from one folder to another after get rid of C:\fakepath?, Please help RRS feed

  • Question

  • User-1355965324 posted

    Sorry for duplicate posting

    When  I try to store a filename from the folder c:\empdoc\emp.pdf  into a textbox , the file name is being showed as  c:\fakepath\emp.pdf. How can I  copy the selected  file    to the folder  H:\empdoc\emp.pdf. My code is given below

     

     

    controller
     public IActionResult DocumentCreate(EmpDocumentModel empdocModel)
            {
    
                
                System.IO.File.Copy(empdocModel.FileUrl, @H:\empdoc\empnewdoc.pdf");              
    
                return View();
            }
    Model
     public class EmpDocumentModel
        {
            public string FileUrl { get; set; }
        }
    html file
    @model ModernTyres.GO.Common.Models.EmpDocumentModel
    
    <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label asp-for="FileUrl" class="control-label control-label-left col-sm-3 text-danger">File Name</label>
                                            <div class="controls col-sm-9">
                                                <div class="input-group">
                                                    <input asp-for="FileUrl" class="form-control" id="fileUrl" maxlength="250" readonly />
                                                    <span class="input-group-btn">
                                                        <button class="btn btn-default" type="button" id="btnSelect">Browse...</button>
                                                    </span>
                                                </div>
                                            </div>
    
    			<div class="col-sm-3">
                                            <div class="center-block ">
                                                <button type="submit" class="btn btn-primary form-control"> <i class="fa fa-save"></i><span class="padding-left-ten">Upload</span></button>
                                            </div>
                                        </div>
                                        </div>
    
                                    </div>
    
    
                                </div>
    
    
    <script>
        $(document).ready(function () {
    
            $("#btnSelect").click(function () {
                var $input = $('<input type="file" />');
                $input.change(function () {
                    $('#fileUrl').val($(this).val());
                    
                });
                $input.trigger('click');
            });
    
        });
    </script>
    Model
      
    
    



    Wednesday, February 5, 2020 9:28 AM

Answers

  • User-1355965324 posted

    I did that different way and it is working fine. Many Thanks

    In model class I defined 

    public IFormFile FileNameURL { get; set; }

    In View

    <input asp-for="FileNameURL" class="form-control custom-file-input">

    In Controller

    string uploadsFolder = @"\\00.00.0.000\Empdoc\pdff\";
    uniqueFileName = Guid.NewGuid().ToString() + "_" + doc.FileNameURL.FileName;
    string filePath = Path.Combine(uploadsFolder, uniqueFileName);

    doc.FileNameURL.CopyTo(new FileStream(filePath, FileMode.Create));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 5, 2020 1:55 PM

All replies

  • User753101303 posted

    Hi,

    Unclear. In short you can't. This is a safety measure to prevent disclosing a client side directory name and it could be changed only by updating client side security settings (it may work on an intranet site).

    Anyway it seems also that you are trying to use this path to copy the file from the client side drive which will never work on a real web server (a web server doesn't have access to client side drives, it just appear to work on a development machine because you are in the very special case where the browser side and the web server side is the same machine and has access to the same disk(s)).

    Instead the file content is "posted" to the web server as a stream of bytes you can read using an upload control or the https://docs.microsoft.com/en-us/dotnet/api/system.web.httprequest.files?view=netframework-4.8 collection. You also have some other requirements for this to work. Please see a tutorial about file uploading.

    Edit: you are currently taking a wrong path, pun intended ;-)

    Wednesday, February 5, 2020 10:08 AM
  • User-1355965324 posted

    I dont clear from your answer. If not possible how can we download the file from other website to our local system .

    Is is possible to copy the file that I have selected  into the text box to another folder?

     

    Wednesday, February 5, 2020 10:16 AM
  • User-1355965324 posted

    What I am trying is 

    Employee related document  would be scanned to one folder of the client machine  eg: c:\temp\emp.pdf, Then  through the application in asp.net core , that scanned  document  should be linked to employee id  and also the document should be copied into the server 10.20.0.000\Empdoc folder . That  server  folder always meant to keep employee document

    Whenever need the employee document can be downloaded and viewed from the client machine.

    I am looking for that functionality. It should be possible in asp.net core but anyone can help me  with suggested code it would be very appreciated 

    Many Thanks

    Pol

    Wednesday, February 5, 2020 10:39 AM
  • User753101303 posted

    Try for example https://www.c-sharpcorner.com/article/upload-download-files-in-asp-net-core-2-0/ (quick find, could perhaps a bit clearer IMO)

    The key point is that a browser and a web server are usually only "talking" through "http" and so either the browser is sending file bytes to the web server as part of the http request or the server is sending file bytes to the browser as part of an http response (and you can trigger a "save dialog" so that the user can save this file somewhere).

    By design a client side machine usually don't have access to server side drives and the web server doesn't have access to client side drives. All goes through http.

    The web server can still access to other reachable servers (but it has been configured first ie you have a communication link in between including correct firewall settings, and a network share was created with the needed permissions so that the web server can then use this network location).

    Wednesday, February 5, 2020 12:13 PM
  • User-1355965324 posted

    I have found some answer like  it is possible using 

    public IFormFile FileNameURL { get; set; } variable in model in Asp.net core and we can use SaveAs method rather than copy.  Please can you help with this and with suggested code

    Wednesday, February 5, 2020 12:20 PM
  • User475983607 posted

    There are several issues with your code.  The code does not compile because it is missing a double quote.

    System.IO.File.Copy(empdocModel.FileUrl, @"H:\empdoc\empnewdoc.pdf"); 

    The code above also assumes the user know directory structure on the server.  That's uncommon and requires that you wrote code that shares the server directory structure.  This type of design might work on your development computer since it is both the server and the client.  But the design will not work once deployed to a remote server.

    The JavaScript shown does not upload files if that's what you are trying to do.

    IMHO, there are too many issues with the code to understand the design intent.

    Wednesday, February 5, 2020 1:03 PM
  • User-1355965324 posted

    I did that different way and it is working fine. Many Thanks

    In model class I defined 

    public IFormFile FileNameURL { get; set; }

    In View

    <input asp-for="FileNameURL" class="form-control custom-file-input">

    In Controller

    string uploadsFolder = @"\\00.00.0.000\Empdoc\pdff\";
    uniqueFileName = Guid.NewGuid().ToString() + "_" + doc.FileNameURL.FileName;
    string filePath = Path.Combine(uploadsFolder, uniqueFileName);

    doc.FileNameURL.CopyTo(new FileStream(filePath, FileMode.Create));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 5, 2020 1:55 PM
  • User475983607 posted

    Have you tested this on a web server and not just your development machine?

    Wednesday, February 5, 2020 2:32 PM
  • User-1355965324 posted

    Yes I published and tested  on webserver. It is working fine. We created a webserver and give access to that server now we can copy the file there.

    Wednesday, February 5, 2020 8:06 PM