upload file wcf [duplicate]

血红的双手。 提交于 2019-12-23 09:07:59

问题


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.

  1. using a fileupload control you can perform the task
  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!