locked
How to upload a file from client machin to server by using webservice RRS feed

  • Question

  • User-2113411087 posted

    I have created webservice using C#.That take filepath/url(object from client pc ,not hosted anywhere),convert to byte array.And using that create a file on server location.Below code working fine on local machine.I uploaded file from C drive to D drive.But when I hosted service  on server not localhost and trying to access,file not get uploaded and getting msg like this : Could not find file 'C:\Fileupload\test.txt'..Client can send only url of file that from local machine not hosted anywhere can not send byte array.Is there any option apart from creating new file in server.Can I directly upload like fileuploadcontrol.Client can either use web app or windows app or windows service for uploading

    Here is my code :

    string uploadFolder = = @"D:\UploadFiles\"; 

    [WebMethod]
    public string UploadFile(string filePath)
    {

    try
    {

    byte[] f = System.IO.File.ReadAllBytes(filePath);
    MemoryStream ms = new MemoryStream(f);

    uploadFolder = uploadFolder + StrFilename;
    // StrFilename extracted from filepath

    FileStream fs = new FileStream(uploadFolder, FileMode.Create);

    ms.WriteTo(fs);
    ms.Close();
    fs.Close();
    fs.Dispose();

    return "OK";
    }

    }
    catch (Exception ex)
    {
    // return the error message if the operation fails
    return ex.Message.ToString();
    }

    }

    Wednesday, October 26, 2016 6:55 AM

Answers

  • User-821857111 posted

    You can't do this. A Server-side process has absolutely no access to the file system of a remote PC. The only reason it works on your machines is because your machine is both client and server.

    If you want to upload files in an ASP.NET application, you have to provide a form with a file upload so the user can browse to a file on their machine and choose to upload it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 26, 2016 7:25 AM

All replies

  • User-821857111 posted

    You can't do this. A Server-side process has absolutely no access to the file system of a remote PC. The only reason it works on your machines is because your machine is both client and server.

    If you want to upload files in an ASP.NET application, you have to provide a form with a file upload so the user can browse to a file on their machine and choose to upload it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 26, 2016 7:25 AM
  • User-2113411087 posted

    Hi Milkesdotnett,thanks for ur reply.Client most probabaly use windows service which take url of file from table.I can not provide web form with fileupload control to upload files

     

    Wednesday, October 26, 2016 7:37 AM