wcf
[WCF] WCF Rest 에서 Image File Upload 하기
동동(이재동)
2011. 6. 16. 11:55
일단 WCF에서 보자
[WebInvoke(UriTemplate = "FileUpload?FileName={fileName}", Method = "PUT")]
public void FileUpLoad(string fileName, Stream fileStream)
{
FileStream fileToupload = new FileStream("d:\\" + fileName, FileMode.Create);
byte[] byteArray = new byte[10000];
int byteRead, totalByteRead = 0;
do
{
byteRead = fileStream.Read(byteArray, 0, byteArray.Length);
totalByteRead += byteRead;
} while (byteRead > 0);
fileToupload.Write(byteArray, 0, byteArray.Length);
fileToupload.Close();
fileToupload.Dispose();
}
일단 이렇게 메소드를 작성해서 file 이름과 stream을 받아서 파일을 경로에 쓴다.
method는 put으로 한다.
자 이제 끝이다. 이제 WPF에서 호출해보자.
public void ImageUpload(string fileName, string filePath)
{
using (WebClient webclient = new WebClient())
{
webclient.UploadData(new Uri(string.Format(serverUri + "/FileUpload?FileName={0}",fileName)), "put", GetData(filePath));
}
}
private byte[] GetData(string filePath)
{
FileStream stream = File.OpenRead(filePath);
byte[] data = new byte[stream.Length];
stream.Read(data, 0, data.Length);
stream.Close();
return data;
}
이렇게 filename과 byte로 변환된 스트림을 WCF로 날려주기만 하면 된다.