- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将文件从 iOS 设备上传到服务器。我想使用 NSURLSessionUploadTask
并且我希望上传从文件中获取正在上传的文件的内容。
服务器期望收到名为“sightings.zip”的文件。
用于上传的 HTML
表单包含一个名为“fileBean”的输入标签,如下所示:
<input name="fileBean" type="file" />
我认为我需要设置请求,以便它包含正确的“Content-disposition
”信息:
Content-Disposition: form-data; name="fileBean"; filename="sightings.zip"
但根据我能找到的示例、有关问题以及 Apple 文档,我不知道如何做到这一点。
我的相关代码如下。
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.allowsCellularAccess = YES;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[operationManager backgroundQueue]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[GGPConstants urlFor:GGP_SEND_SIGHTINGS_PATH]];
[request setHTTPMethod:@"POST"];
[request setValue:@"identity" forHTTPHeaderField:@"Accept-Encoding"];
[request setValue:@"Content-Type" forHTTPHeaderField:@"application/zip"];
[request addValue:@"sightings.zip" forHTTPHeaderField:@"fileName"];
// How to get the value for the name and filename into the content disposition as per the line below?
// Content-Disposition: form-data; name="fileBean"; filename="sightings.zip"
NSURLSessionUploadTask *uploadTask = [session
uploadTaskWithRequest:request
fromFile:myFileURL
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
// Celebrate the successful upload...
}
}];
过去我使用过AFNetworking
AFHTTPRequestSerializer
,它在构建表单数据时提供输入名称,但由于其他原因这对我不起作用。
如有任何帮助,我们将不胜感激。
最佳答案
我不推荐使用form-data类型,因为它很容易出错。最好使用 application/x-www-form-urlencoded,它的构造非常简单。在这种格式中,正文数据基本上看起来像 GET 请求,但没有问号,例如
name=foo.jpg&data=[url-encoded data blob here]
其中可以按照 Apple's docs 中的描述生成 url 编码的数据 blob添加了几个 (__bridge_transfer NSString *) 和 (__bridge CFStringRef) 位。 :-)
也就是说,有一个 decent example Stack Overflow 上已经有多部分表单数据了。请注意,当您实际使用它时,您会在边界的前面添加两个额外的连字符,因此如果您指定“foo”作为您的边界,那么您将在每个部分之间有“--foo”。
关于ios - 使用 NSURLSessionUploadTask fromFile,如何将 HTML 输入名称与正在上传的文件链接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37739642/
我不明白为什么有两段代码要做完全相同的事情,却在 Scala 中做不同的事情。 第一个例子: scala> val ggg = Source.fromFile("/somefile"); ggg: s
我想创建一个 Windows 窗体,它从文件中获取图像并显示在 C# 的 pictureBox 中当我在“=”之后键入 image.FromFile 时遇到问题,FromFile 变成红色下划线,就好
我使用 Image.FromFile() 读取用户上传的图像文件。对于“选择文件”对话框,我可以允许哪些文件扩展名? 谢谢! 最佳答案 来自微软的文档:http://msdn.microsoft.co
我想知道以下代码片段的效率是多少: val lst = Source.fromFile(f).getLines.toList 发出lst.contains(x)时, 这是否意味着f正在被重新扫描,或者
我可能想在这里重新发明轮子,但是 numpy 有一个 fromfile() 函数,可以读取 - 我想 - CSV 文件。 它的速度似乎令人难以置信 - 即使与 Pandas read_csv() 相比
我正在尝试更新一些在方法中使用 np.fromfile 的旧代码。当我尝试在 numpy 源中搜索此方法时,我只找到 np.core.records.fromfile ,但是当您搜索文档时,您可以找到
我正在尝试向 numpy.fromfile 传递一个模拟的 file,但我不断收到以下错误: IOError: !first argument must be an open file 这是一个基本示
np.fromfile() 是否有最大文件大小?我正在尝试读取一个 48GB 的文件,其中包含大约 2.1e9 条记录(每条记录有 5 个值)。 当我将计数限制为 8e7 时它起作用了: from
我正在尝试使用 numpy.fromfile阅读structured array (文件头)通过传入 user defined data-type .出于某种原因,我的结构化数组元素以二维数组而不是平
这个问题可能是微不足道的,但我真的不知道如何解决它。假设我使用 np.int64 类型生成一个大小为 (4,3) 的 numpy 数组,然后写入二进制文件。该数组如下所示: [[ 1 2 3]
我有一个带有 xfce 窗口管理器的系统 fedora 15。 我安装了一个 inotify 工具来玩。 我想控制我的文件在我的工作过程中会发生什么。我今天使用一个命令来运行 inotify inot
我有一些大文件(甚至压缩到 10GB 左右),其中包含一个 ASCII header ,然后原则上每个大约 3MB 的 numpy.recarrays,我们称它们为“事件”。我的第一种方法是这样的:
为什么我会遇到内存不足的错误?谢谢 if (File.Exists(photoURI)) { FileStream fs = new FileStream(photoURI, FileMode
我有一个二进制文件,其中包含飞机位置的记录。每条记录如下所示: 0x00: Time, float32 0x04: X, float32 // X axis position 0x08: Y, flo
我写的文件传输代码如下: val fileContent: Enumerator[Array[Byte]] = Enumerator.fromFile(file) val size = file.le
全部, 我有一个只有 ~120MB 的 CSV 文件(将其命名为 demo.csv) 以下代码会导致堆从正常大小的 100MB 膨胀到 1.7GB,尽管加载的底层数据只有 120MB 我可以在哪些方面
我在这种情况下使用 Image.FromFile(string) 方法: using System; using System.Collections.Generic; using System.Co
它和我想象的一样奇怪。 o.0. 我已经检查并仔细检查了,甚至不明白为什么它不起作用... 下面是使用的代码: try { if (!(Directory.Exists(@"C:\Simple
我发现了几个类似的问题,但没有一个能准确描述我面临的问题。我有一个 OBJ 文件,其中包含描述顶点位置的行: v 0.01214 0.4242 0.82874 我想将此数据转换为 matlab 友好的
我正在使用 numpy.fromfile 构建一个数组,我可以将其传递给 pandas.DataFrame 构造函数 import numpy as np import pandas as pd de
我是一名优秀的程序员,十分优秀!