- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 RestSharp(Visual Studio 2013 中的版本 105.2.3.0,.net 4.5)来调用 NodeJS 托管的 Web 服务。我需要调用的电话之一是上传文件。使用 RESTSharp 请求,如果我将流从我的一端检索到一个字节数组中并将其传递给 AddFile,它就可以正常工作。但是,我更愿意流式传输内容,而不是将整个文件加载到服务器内存中(文件可以是 100 MB)。
如果我设置了一个操作来复制我的流(见下文),我会在 System.Net.ProtocolViolationException 的“MyStream.CopyTo”行出现异常(要写入流的字节超过指定的 Content-Length 字节大小) .在调用 client.Execute 后,此异常会在 Action block 内引发。
根据我的阅读,我不应该手动添加 Content-Length header ,如果我这样做也无济于事。我尝试将 CopyTo 缓冲区设置得太小和太大,以及完全忽略它,但无济于事。有人可以告诉我我错过了什么吗?
// Snippet...
protected T PostFile<T>(string Resource, string FieldName, string FileName,
string ContentType, Stream MyStream,
IEnumerable<Parameter> Parameters = null) where T : new()
{
RestRequest request = new RestRequest(Resource);
request.Method = Method.POST;
if (Parameters != null)
{
// Note: parameters are all UrlSegment values
request.Parameters.AddRange(Parameters);
}
// _url, _username and _password are defined configuration variables
RestClient client = new RestClient(_url);
if (!string.IsNullOrEmpty(_username))
{
client.Authenticator = new HttpBasicAuthenticator(_username, _password);
}
/*
// Does not work, throws System.Net.ProtocolViolationException,
// Bytes to be written to the stream exceed the
// Content-Length bytes size specified.
request.AddFile(FieldName, (s) =>
{
MyStream.CopyTo(s);
MyStream.Flush();
}, FileName, ContentType);
*/
// This works, but has to load the whole file in memory
byte[] data = new byte[MyStream.Length];
MyStream.Read(data, 0, (int) MyStream.Length);
request.AddFile(FieldName, data, FileName, ContentType);
var response = client.Execute<T>(request);
// check response and continue...
}
最佳答案
以下代码适用于我使用 rest sharp 上传 csv 文件。已调用 Web 服务 API。
var client = new RestClient(<YOUR API END URL >);
var request = new RestRequest(Method.POST) ;
request.AlwaysMultipartFormData = true;
request. AddHeader("Content-Type", "multipart/form-data");
request.AddHeader("X-API-TOKEN", <Your Unique Token - again not needed for certain calls>);
request.AddParameter(<Your parameters.....>);
request.AddFile("file", currentFileLocation, contentType);
request.AddParameter("multipart/form-data", fileName, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var response = client.Execute(request);
关于c# - RestSharp AddFile 使用 Stream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32876606/
我在多个页面上使用 Coldfusion Grid 程序时遇到问题,网格无法显示。最初我们得到一个“'EXT' 是未定义的错误”,但是在更新我们的脚本文件时我们现在得到一个“对象不支持属性或方法'Ad
因此,当我单击 addFiles 按钮时,我在 firebug 中收到一个错误,内容为 "TypeError: addFiles is not a function." 这对我来说没有任何意义,因为如
我正在使用 RestSharp 的 AddFile,它工作得很好,除了我的文件由于添加的这个头信息而最终被破坏。 -------------------------------289477580292
我尝试在集群上运行 sc.texfile("file:///.../myLocalFile.txt"),但出现了 java.io.FileNotFoundException在 worker 身上。 所
我想通过 Windows Azure 上托管的 Web 服务从我的 Windows Phone 发送图片。为了与我的服务进行通信,我使用 RESTSharp,并且我看到有一个名为 addFile 的方
我正在使用 RestSharp(Visual Studio 2013 中的版本 105.2.3.0,.net 4.5)来调用 NodeJS 托管的 Web 服务。我需要调用的电话之一是上传文件。使用
我一直在成功地将 pyspark 与我的 YARN 集群一起使用。我的工作 这样做涉及使用 RDD 的管道命令通过二进制发送数据 我做了。我可以像这样在 pyspark 中轻松做到这一点(假设 'sc
我正在使用 dropzone 将图像上传到画廊。我正在通过按钮提交。是否可以防止两次添加相同的文件?我不太确定是检查名称还是名称和大小。这是我的代码: var i = 0;
很明显,为了更好地分发小型查找数据,使用广播变量。 假设我们在yarn客户端模式下从主节点运行pySpark代码(spark提交)。因此应用程序驱动程序将始终在主节点上创建。我们从主节点上的本地路径读
我想了解 hadoop 是如何工作的。假设我在 hdfs 上有 10 个目录,它包含 100 个我想用 spark 处理的文件。 在书中 - 使用 Spark 进行快速数据处理 这要求文件在集群中的所
我正在尝试使用 sc.addFile 将文件添加到所有工作节点。但是,我发现它无法覆盖同一个文件。有什么方法可以通过 sc.addFile 覆盖文件吗? 最佳答案 配置为spark.files.ove
我正在尝试使用 sc.addFile 将文件添加到所有工作节点。但是,我发现它无法覆盖同一个文件。有什么方法可以通过 sc.addFile 覆盖文件吗? 最佳答案 配置为spark.files.ove
我在核心模式下使用 FineUploader(它工作得非常好——我怎么推荐都不为过)。 当我从代码中的其他地方调用 addFiles(file) 时,我想在 uploader 中找出分配给该文件的 I
我在这方面已经有一段时间了。这实际上工作了一次,然后就再也没有了。它根本不会创建 zip 文件。该文件确实存在。 $zip = new ZipArchive(); $filename = "./tes
本文整理了Java中net.lingala.zip4j.core.ZipFile.addFile()方法的一些代码示例,展示了ZipFile.addFile()的具体用法。这些代码示例主要来源于Git
本文整理了Java中net.lingala.zip4j.core.ZipFile.addFiles()方法的一些代码示例,展示了ZipFile.addFiles()的具体用法。这些代码示例主要来源于G
本文整理了Java中cn.hutool.core.util.ZipUtil.addFile()方法的一些代码示例,展示了ZipUtil.addFile()的具体用法。这些代码示例主要来源于Github
本文整理了Java中org.dihedron.core.zip.ZipArchive.addFile()方法的一些代码示例,展示了ZipArchive.addFile()的具体用法。这些代码示例主要来
perl Digest模块为 add 计算不同的 SHA1 摘要和 addfile职能。 我使用 /dev/urandom 创建了二进制随机数据 在 ubuntu 上运行 $ lsb_release
本文整理了Java中org.codehaus.plexus.archiver.zip.ZipArchiver.addFile()方法的一些代码示例,展示了ZipArchiver.addFile()的具
我是一名优秀的程序员,十分优秀!