iphone - 如何在 Objective-C 中的 Web 服务调用中将文件作为参数传递?

标签 iphone ios web-services ipad

我有一个本地存储的文件,我想使用 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/

相关文章:

ios - Cocoapods 每次都需要通过 dropbox 同步在不同的计算机上更新 bolts 框架

ios - 查看 Controller 层次结构和设备方向变化

javascript - 通过 JavaScript 访问 REST Web 服务

c++ - 网络服务调度员

iphone - NSURLConnection缓存和接收数据

ios - 关闭应用程序并再次打开,然后 PopToRootViewController 无法在 swift firebase 中工作

iphone - 如何在 iPhone 横向模式下删除 UITableview 滚动中的空白?

iphone - 如何可靠地找到 UIGestureRecognizer 的正确 View ?

iphone - 在iOS中将GMT转换为IST

ios - 从 iOS 应用程序向 SOAP ASMX 服务发送参数