我有一个本地存储的文件,我想使用 copy.asmx 的“CopyInToItems”Web 服务将其上传到共享点服务器。我必须将文件作为 NSStream 传递。 我能弄清楚该怎么做吗?
<!--Request-->
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsi="http>
<soap:Body>
<CopyIntoItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<SourceUrl>-------local path of the file-------</SourceUrl><DestinationUrls>
<string>-------server path where the file needs to be uploaded--------</string>
</DestinationUrls>
<Fields>
<FieldInformation Type="Invalid" Id="00000000-0000-0000-0000-000000000000" />
</Fields>
<Stream>
----File Stream----
</Stream>
</CopyIntoItems>
</soap:Body>
</soap:Envelope>
提前致谢!
最佳答案
其实你不需要使用NSSTREAM。您可以使用以下方法将本地存储的文件转换为 NSDATA:
NSMutableData *filedata=[[NSMutableData alloc]initWithContentsOfURL:[NSURL URLWithString:localPath]] ;
然后使用 base-64 编码将此 NSDATA 转换为 NSSTRING。 由于 iOS 中无法直接完成 Base-64 编码,因此这里有一个函数可以实现此目的。
- (NSString *) base64StringFromData: (NSData *)data length: (int)length {
unsigned long ixtext, lentext;
long ctremaining;
unsigned char input[3], output[4];
short i, charsonline = 0, ctcopy;
const unsigned char *raw;
NSMutableString *result;
lentext = [data length];
if (lentext < 1)
return @"";
result = [NSMutableString stringWithCapacity: lentext];
raw = [data bytes];
ixtext = 0;
while (true) {
ctremaining = lentext - ixtext;
if (ctremaining <= 0)
break;
for (i = 0; i < 3; i++) {
unsigned long ix = ixtext + i;
if (ix < lentext)
input[i] = raw[ix];
else
input[i] = 0;
}
output[0] = (input[0] & 0xFC) >> 2;
output[1] = ((input[0] & 0x03) << 4) | ((input[1] & 0xF0) >> 4);
output[2] = ((input[1] & 0x0F) << 2) | ((input[2] & 0xC0) >> 6);
output[3] = input[2] & 0x3F;
ctcopy = 4;
switch (ctremaining) {
case 1:
ctcopy = 2;
break;
case 2:
ctcopy = 3;
break;
}
for (i = 0; i < ctcopy; i++)
[result appendString: [NSString stringWithFormat: @"%c", base64EncodingTable[output[i]]]];
for (i = ctcopy; i < 4; i++)
[result appendString: @"="];
ixtext += 3;
charsonline += 4;
if ((length > 0) && (charsonline >= length))
charsonline = 0;
}
return result;
}
现在调用此函数并传递您之前创建的文件的 NSDATA 作为参数。
NSString *stream=[self base64StringFromData:filedata length:[filedata length] ];
现在您可以将此字符串作为参数传递到 Web 服务调用中
关于iphone - 如何在 Objective-C 中的 Web 服务调用中将文件作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10518001/