如何通过 REST 服务发送本地文件?

How to send a local file through a REST service?

我正在使用 WCF 和 C# (VS 2010) 开发 REST Web 服务。我想开发这样的操作:

doSomethingWithAFile(String filePath)

所以它会像这样调用:

GET http://my.web.service/endpoint?filePath={filePath}

filePath 是客户端(不是服务器)中的文件路径。因此,在调用时,该操作必须将路径指向的文件发送到服务器,以便服务器可以对文件中包含的数据进行一些操作。

我怎样才能做到这一点?

编辑:正如我在评论中所述,我会在客户端设置一个共享文件夹,所以我发送路径,服务器读取文件夹中的文件。

在您的服务器上,您必须有一个服务,该服务具有接受字符串输入的方法,您可以使用客户端应用程序的文件路径调用该方法。
然后,您 read/copy/whichever 通过普通文件 IO 方法在您的服务器上从该位置获取文件。

您可以在下面找到有关如何执行此操作的示例。 ServerPleaseFetchThisFile的定义自然要看是什么类型的webservice,WCF还是IIS web service还是自制的web service。

public bool ServerPleaseFetchThisFile(string targetPath)
{
  // targetPath should enter from the client in format of \Hostname\Path\to\the\file.txt
  return DoSomethingWithAFile(targetPath);
}

private bool DoSomethingWithAFile(string targetFile)
{
  bool success = false;

  if (string.IsNullOrWhiteSpace(targetFile))
  {
    throw new ArgumentNullException("targetFile", "The supplied target file is not a valid input.");
  }

  if (!File.Exists(targetFile))
  {
    throw new ArgumentNullException("targetFile", "The supplied target file is not a valid file location.");
  }

  try
  {
    using (FileStream targetStream = new FileStream(targetFile, FileMode.Open, FileAccess.Read))
    {
      // Do something with targetStream
      success = true;
    }
  }
  catch (SecurityException se)
  {
    throw new Exception("Security Exception!", se);
    // Do something due to indicate Security Exception to the file
    // success = false;
  }
  catch (UnauthorizedAccessException uae)
  {
    throw new Exception("Unathorized Access!", uae);
    // Do something due to indicate Unauthorized Access to the file
    // success = false;
  }

  return success;
}