Can some please help in understanding what is going wrong???
I will have upload my .db file to server using multipart/formdata request.
I'm using ASIFormdataRequest to form the request and i'm getting the response as success.But when I try to download the file I'm getting disk image malformed.
I'm pasting the code
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSString* path = [self FilePath];
NSData *dataBaseData = [NSData alloc];
dataBaseData = [NSData dataWithContentsOfFile:path];
NSLog(#"databaselength %d",dataBaseData.length);
NSString* authHeader = [self returnAuthHeader];
NSLog(#"Auth Header %#", authHeader);
[request addRequestHeader:#"Accept" value:#"application/xml"];
[request addRequestHeader:#"Authorization" value:authHeader];
[request addRequestHeader:#"Content-Type" value:#"multipart/form-data"];
// [request setFile:dataBaseData withFileName:#"sample.db" andContentType:#"multipart/form-data" forKey:#"filedata"];
[request addData:dataBaseData withFileName:#"sample.db" andContentType:#"application/octet-stream" forKey:#"filedata"];
[request setPostFormat:ASIMultipartFormDataPostFormat];
[request setDelegate:self];
NSLog(#"Request %#",request);
[request updateUploadProgress];
[request startAsynchronous];
Found why db got corrupted and gave Disk image malformed!!!!!!
Was reading the db in a file in one thread and inserting values to the tables in the db in another thread.So the integrity of the db was not maintained.
Related
i have tried to send username and password along with person image to server using ASIHTTPRequest and i have written some code but it's not working data is not sent to server please some body help me with example code
-(void)service_Call{
NSString *urlStr = [NSString URLWithString:urlstring];
NSLog(#"urlStr --->> %#",urlStr);
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];
[request setRequestMethod:#"POST"];
[request setPostValue:self.FnameTxt.text forKey:#"username"];
[request setPostValue:self.LnameTxt.text forKey:#"password"];
NSString *fileName = #"iphone.jpg";
// Upload an image
UIImage *img = [UIImage imageNamed:fileName];
NSData *imageData = UIImageJPEGRepresentation(img, 90);
[request setData:imageData withFileName:fileName andContentType:#"image/jpeg" forKey:#"image"];
[request setDelegate:self];
[request startAsynchronous];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
NSLog(#"response: %#", responseString);
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
But data is not sending to server please help me somebody if there is any error i code
You should use AFNetworking instead of ASIHTTPRequest, and you will find how handy it can be used.
Also, normally imageData should be sent as base64 string, if you send image by NSData, how your server side handle it?
I came into an issue trying to send files with NSURLSessionUploadTask to a custom REST service.
The problem is that the file seems to get transferred up to an arbitrary size, and then the task stops without any error (URLSession:task:didCompleteWithError: gets called with error set to nil).
The file that needs to be transferred is almost 10MB in size, and I found that smaller files sometimes get transferred correctly.
The code I'm using for creating the task is the following:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setValue:#"application/json; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"100-continue" forHTTPHeaderField:#"Expect"];
[request setHTTPMethod:#"POST"];
[request setValue:[NSString stringWithFormat:#"%d", length] forHTTPHeaderField:#"Content-Length"];
NSURL *fileUrl = [NSURL fileURLWithPath:instanceFilePath];
NSURLSessionUploadTask *uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileUrl];
I need to specify some headers for the server to correctly interpret the request, and the length variable is the effective size of the file being sent.
Any idea about what's going on here?
Thanks.
Hi I am downloading an epub file from my server of size (100-200 MB)
epub file is basically a zip file with extension ".epub"
Code which I am using to download the file:
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:TempBookUrl]];
bookZipPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *tempStr = [bookZipPath stringByAppendingPathComponent:[NSString stringWithFormat:#"temp%#.epub",model.iD]];
NSString *str = [bookZipPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.epub",model.iD]];
[request setTemporaryFileDownloadPath:tempStr];
[request setDownloadDestinationPath:str];
[request setDidFinishSelector:#selector(processFile:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
[request setDelegate:self];
[request setAllowResumeForFileDownloads:YES];
[request setShowAccurateProgress:YES];
[request setDownloadProgressDelegate:self];
[request setShouldContinueWhenAppEntersBackground:YES];
[request startAsynchronous];
After lots of downloading I get the error
saying that unable to decompress the file as file do not exist at the temporary path.
ERROR: Decompression of /Users/admin/Library/Application
Support/iPhone
Simulator/7.0/Applications/07322021-F878-41DA-A712-5C175C5252B6/Documents/temp27.epub
failed the file does not exist
I am using iOS 7
I want to upload image to server from iphone. Using ASIHttpRequest and ASIFormDataRequest i have sucessfully uplaoded picture to server by
[request setdata:...]
But I also want to send userId to server with image. How to achieve this? Here is my code...
ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:URLSaveImage]];
__unsafe_unretained ASIFormDataRequest *request = _request;
[ASIHTTPRequest setShouldThrottleBandwidthForWWAN:YES];
[request setUploadProgressDelegate:self];
NSData *data=UIImageJPEGRepresentation(proImage.image,1.0);
[request setData:data
withFileName:#"profile.jpg"
andContentType:#"image/jpeg"
forKey:#"file"];
For POST method, use setPostValue: forKey: with the setdata:. You dont need to do any additional work . Just post the variable along with image.
[request setPostValue:VALUE forKey:KEY];
[request setData:data withFileName:#"profile.jpg" andContentType:#"image/jpeg"
forKey:#"file"];
I want to upload follow Log Strings along with UIImage,
HOw can I achieve such objective????
I can see to send only image , but i want to send image along with text data using this ASIFormDataRequest, using POST request type.
NSLog(#"data is here %#\n%#\n%#\n%#\n%#\n",deviceID, postID, name, message, picture);
UIImage *image= [UIImage imageNamed:#"profile_avatar.png"];
NSString *strURL = #"https://abc.abc.com/comments/create";
// ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]]; // Upload a file on disk
// NSString *filename1=[NSString stringWithFormat:#"friendship.jpg"];
UIImage *image1=image;
NSData *imageData1=UIImageJPEGRepresentation(image1, 1.0);
[request setData:imageData1 withFileName:#"filename" andContentType:#"image/png" forKey:#"avatar"];
[request setRequestMethod:#"POST"];
[request addRequestHeader:#"Authorization" value:[NSString stringWithFormat:#"Basic %#",[ASIHTTPRequest base64forData:[[NSString stringWithFormat:#"username123:pass123"] dataUsingEncoding:NSUTF8StringEncoding]]]];
[request setValidatesSecureCertificate:NO];
//[request appendPostData:body];
[request setDelegate:self];
[request setTimeOutSeconds:3.0];
request.shouldAttemptPersistentConnection = NO;
[request setDidFinishSelector:#selector(uploadRequestFinished:)];
[request setDidFailSelector:#selector(uploadRequestFailed:)];
[request startAsynchronous];
Thanks
There is one method available to send parameter as well
[request setPostValue:#"YourValue" forKey:#"YourKey"];