I have now got creation of a text file on a web server from a string working using the following code:
(void) UploadFileContent: (NSString *) fileContent{
// URL for script file on server in folder where file will be created
NSString *urlString = @"https://xxxx/xxxx/Upload.aspx"
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// UploadedFile.txt is name of file to be created
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"UploadedFile.txt\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:fileContent] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set the body of the post to the request
[request setHTTPBody:body];
// connect to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
}
The Upload.aspx file is
%@ Import Namespace="System"%
%@ Import Namespace="System.IO"%
%@ Import Namespace="System.Net"%
%@ Import NameSpace="System.Web"%
Script language="C#" runat=server
void Page_Load(object sender, EventArgs e) {
foreach(string f in Request.Files.AllKeys) {
HttpPostedFile file = Request.Files[f];
// FolderPath = path to folder containing file to be created eg C:\inetpub\wwwroot\xxx\xxx\xxx\
string sFilePath = @"FolderPath" + file.FileName;
if(System.IO.File.Exists(sFilePath)) System.IO.File.Delete(sFilePath);
file.SaveAs(sFilePath);
}
}
/Script
html
body
p Upload complete. /p
/body
/html