You must Sign In to post a response.
  • Category: Webservices

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

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

    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();
    }

    }
  • #768183

    Hi,

    I am not sure whether this task will work for this code. Because you are trying to push the file to new directory?
    But your current directory was different.
    Even if it works in local once if you have done your deployment there is lot of possibilities that it get failed in server.
    Because Server-side process has absolutely no access to the file system of a remote PC.

    I feel it better to change your complete code or we can wait for some expert to give us solution.


    Thanks,
    Mani


Sign In to post your comments