下面的代码我使用asmx webservice将文件作为字节数组获取,并希望将其保存到iphone应用程序的文档目录
我请求的文件是sqlite文件
我的代码是:
-(void)getfile{
NSString* soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> <soap:Body> <GetDocument xmlns=\"http://tempuri.org\"> <DocumentName>%@</DocumentName> </GetDocument></soap:Body> </soap:Envelope>", @"ErtugrulGuler"];
NSURL* theUrl = [NSURL URLWithString:@"http://test.xxxx.com/xxxxx/xxxx.asmx"];
NSString* msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
NSMutableURLRequest* theRequest = [NSMutableURLRequest requestWithURL:theUrl];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http://tempuri.org/GetDocument" forHTTPHeaderField:@"SOAPAction"];
[theRequest setHTTPMethod:@"POST"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connect = [[NSURLConnection alloc] initWithRequest: theRequest delegate:self];
if (connect)
{
}
else {
NSLog(@"No Connection established");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
downloadData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[downloadData appendData:data];
NSString *receivedDataString = [[NSString alloc] initWithData:downloadData encoding:NSUTF8StringEncoding];
NSLog(@"data string: %@",receivedDataString);
NSLog(@"data length: %d",[data length]);
//NSLog(@"bytesWritten == %d error == %@",bytesWritten,[fileOutputStream streamError]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
//NSLog(@"finish loading");
[self performSelector:@selector(downloadData:) withObject:downloadData afterDelay:0];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var
}
-(void)downloadData:(NSData*)response
{
//NSLog(@" \n\n STRING = %@ \n\n ",[[NSString alloc]initWithData:response encoding:NSUTF8StringEncoding]);
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir=[paths objectAtIndex:0];
NSString *documentFile=[documentDir stringByAppendingPathComponent:@"abc.sqlite"];
NSString * yourAppendingText=[[NSString alloc]initWithData:response encoding:NSUTF8StringEncoding];
[yourAppendingText writeToFile:documentFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
这是我的webservice方法,返回字节数组
[WebMethod]
public Byte[] GetDocument(string DocumentName)
{
string strdocPath;
strdocPath = "path" + DocumentName;
FileStream objfilestream = new FileStream(strdocPath,FileMode.Open,FileAccess.Read);
int len = (int)objfilestream.Length;
Byte[] documentcontents = new Byte[len];
objfilestream.Read(documentcontents,0,len);
objfilestream.Close();
return documentcontents;
}
此代码连接成功但我不知道如何使用NSData获取文件并将文件写入文档目录为.sqlite
@EDIT: 我解决了一些问题,但我想得到的文件是53kb,我收到的数据字符串如下:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetDocumentResponse xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>
和数据长度:297因此它无法获取我认为的数据
在asp.net中运行服务并成功获取数据。所以服务没有问题,但可能会增加ios的最大尺寸?但怎么样?
答案 0 :(得分:1)
NSOutputStream *fileOutputStream;
NSString *databasePath = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),@"database.sqlite"];
fileOutputStream = nil;
fileOutputStream = [[NSOutputStream alloc] initToFileAtPath:databasePath append:NO];
[fileOutputStream open];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
int bytesWritten = [fileOutputStream write:[data bytes] maxLength:[data length]];
NSLog(@" %s bytesWritten == %d error == %@",__FUNCTION__,bytesWritten,[fileOutputStream streamError]);
}