0

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

提前致谢!

4

1 回答 1

1

实际上你不需要使用 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 服务调用

于 2012-05-11T07:59:48.200 回答