2
我想上傳存儲在我的應用程序文檔文件夾中的我的文件(不同格式)。我在桌面視圖中列出了所有文件,並選擇了一行,我嘗試上傳該行中的文件。 這裏是我的代碼如何將文件上傳到存儲在ios文檔文件夾中的服務器?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *urlString;
if (indexPath.section == 0)
{
fileUrlToUpload = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:documents[indexPath.row] ofType:nil]];
}
else
{
fileUrlToUpload = [self.documentURLs objectAtIndex:indexPath.row];
}
[self setupDocumentControllerWithURL:fileUrlToUpload];
urlString = [[fileUrlToUpload path] lastPathComponent];
printf("\n the string is :%s",[urlString UTF8String]);
[self publishToServer:urlString];
-(void)publishToServer:(NSString *)str
{
if([str length] > 0)
{
NSString *[email protected]"";
urlPath= str;
NSString *urlString = @"http://162.108.0.8:8080/Path/upload_path.jsp?";
NSMutableURLRequest *request =[[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"path\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:urlPath] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--",boundary] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
[request setHTTPBody:postBody];
NSLog(@"request === %@",request);
NSData *returnData =nil;
returnData= [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"returnData === %@",returnData);
NSString * string=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
}
1)當我試圖從後模擬器空值在服務器數據庫中存儲的文件,我收到「成功上傳」作爲響應。
2)當我試圖從後我沒有得到服務器甚至可以爲null值的設備文件。
請幫幫我。
謝謝
請任何一個幫助我。我真的很困惑。是否無法將.txt,.html,.pdf,.jpg文件上傳到服務器 – user1179590