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

    Upload file using asp.net webservice

    Hello I want to upload file from webservice not WCF.I am using C# and consuming it with web application.From web application send file,and service will accept that file(text file) and place in upload folder of website/or given location.

    For this I have created Like this

    : For making webservice

    created empty web application -> selected new Item -> Web service

    1> Wrote following code in webservice

    [WebMethod]
    public void UploadFile()
    {
    FileStream targetStream = null;

    Stream sourceStream = FileByteStream;

    string uploadFolder = @"D:\UploadFile";

    string filePath = Path.Combine(uploadFolder, @"C:\Users\maya\Desktop\test.txt");

    using (targetStream = new FileStream(filePath, FileMode.Create,
    FileAccess.Write, FileShare.None))
    {
    //read from the input stream in 65000 byte chunks

    const int bufferLen = 65000;
    byte[] buffer = new byte[bufferLen];
    int count = 0;
    while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
    {
    // save to output stream
    targetStream.Write(buffer, 0, count);
    }
    targetStream.Close();
    sourceStream.Close();
    }
    }
    Here my not taking any input,I have manual entered one text file.I want to transfer that file to uploadfolder. I am getting error(HTTP 500 Internal server error) at this line.while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0).How to handle this.
  • #768152
    is the file is already present on server or on client ? from where you want to picked it ? if the file is already present on client, then you can not directly upload it without file selection it may treat as malicious activity.
    Web service is always run on IIS (means on server) it does not have a access of Client machine.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments