Nsmutableurlrequest: caricare un file utilizzando Sethttpbodystream piuttosto che sethttpbody

StackOverflow https://stackoverflow.com//questions/11692275

Domanda

Attualmente caricando i file tramite il post HTTP, utilizzando [body appendData: [NSData dataWithData:data]]; in cui i dati sono la mia rappresentazione NSmutabilitata del file.

// Set content type to be form data
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

// Set content type to be form data
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

// Set up web request with HTTP post headers for 1 form field which is the image
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding: NSUTF8StringEncoding]];
[body appendData: [NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding: NSUTF8StringEncoding]];

// Set the body
[request setHTTPBody: body];
.

A causa delle limitazioni della memoria, ho letto che è meglio caricare utilizzando setHTTPBodyStream e passando nella posizione del file on Disk - Come dovrei andare a impostare altri attributi come la disposizione del contenuto utilizzando questo metodo?

È stato utile?

Soluzione

È necessario preparare un file contenente l'intero corpo di richiesta HTML, inclusi i limiti multipart, il tipo di contenuto e la disposizione e l'uso di setHTTPBodyStream.

Ci sono alternative (subclassing NSInputStream, che sembra essere un po 'complicato e biblioteche di terze parti come Asihttprequest). Questo post fornisce ulteriori informazioni e collegamenti.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top