问题
I'll create an WCF for uploading file such as images or pdf files to te server. How can I create a service that can handle this function ? I tried to googling about it, but most of article told me to use Stream as service parameter. But what I want is using byte[] (array) for the file content. because, this service is not only accessing using .nte framework, but also using other technologies, such as php, java, objective-c, etc.
any helps ?
回答1:
Seems streaming is your only option. See this [MSDN example]
See this question : How to upload a file to a WCF Service?
You could check out this article: http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx
It talks about just setup WCF Service for receiving arbitrary data, and you can POST from any client (php,java etc)
回答2:
Create a WCF service method accepting byte[]
as a parameter:
[OperationContract]
public void ReceiveByteArray(byte[] byteArray) { ... }
回答3:
Create a WCF Service method accepting File Stream.
- using a fileupload control you can perform the task
- create a Temp folder on client site.
here code...
string fileextension = null, FileName = null;
try
{
if (FileUpload1.HasFile)
{
ITransferFile clientUpload = new TransferFileClient();
RemoteFileInfo uploadRequestInfo = new RemoteFileInfo();
fileextension = Path.GetExtension(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath(Path.Combine("~/TempFolder/", FileName + fileextension)));
System.IO.FileInfo fileInfo = new System.IO.FileInfo(Server.MapPath("~/TempFolder/") + FileName + fileextension);
using (System.IO.FileStream stream = new System.IO.FileStream(fileInfo.FullName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
uploadRequestInfo.FileName = FileUpload1.PostedFile.FileName;
uploadRequestInfo.Length = fileInfo.Length;
uploadRequestInfo.FileByteStream = stream;
clientUpload.UploadFile(uploadRequestInfo);
}
}
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write("Error : " + ex.Message);
}
finally
{
if (File.Exists(Server.MapPath("~/TempFolder/") + FileName + fileextension))
{
File.Delete(Server.MapPath("~/TempFolder/") + FileName + fileextension);
}
}
来源:https://stackoverflow.com/questions/7804469/upload-file-wcf