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 specific location.
For this I have created webservice like this
: For making webservice
Created empty web application -> selected new Item -> Web service
1> Wrote following code in webservice
public System.IO.Stream FileByteStream;
[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 this error:
HTTP 500 Internal server error
at this line:
while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
How to handle this?
Best Answer
I use an aspx page instead of a webservice.I use jquery to do so. It works like a charm.
so let this be your file upload control.
Here is jquery code
And this is my aspx code