Come posso caricare sul server alcune informazioni di testo (una stringa di testo) e un file di immagine sulla stessa richiesta di post http. Ho caricato le immagini da solo, ma non riesco a far funzionare il testo con esso. Grazie!Come inviare dati di post e file di immagine al server Xcode
risposta
Usa questo codice per caricare immagini e textLabel
NSData *imageData = UIImageJPEGRepresentation("yourImage",0.2); //change Image to NSData
if (imageData != nil)
{
filenames = [NSString stringWithFormat:@"TextLabel"]; //set name here
NSLog(@"%@", filenames);
NSString *urlString = @"http://xxxxxxx/yyyyy.php";
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 *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
[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:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
NSLog(@"finish");
}
Nell'uso lato php questo codice
$myparam = $_POST['userfile']; //getting image Here
$mytextLabel= $_POST['filenames'] //getting textLabe Here
echo $myparam;
echo $mytextLabel;
$target_path = "uploads/";
$target_path = $target_path . basename($_FILES['myfile']['name']);
if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
echo "The file ". basename($_FILES['myfile']['name']) . " has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}
Non posso dire dove viene impostata l'etichetta di testo qui, diciamo che il testo è "buongiorno". Dove dovrebbe andare in questo codice? – sebi
Ho aggiornato la mia risposta .. – akk
Ok grazie capito, btw non ho usato nessuno dei nomi di asset, o caratteri, perché non sono sicuro di cosa faccia. – sebi
Basta aggiungere seguente metodo On pulsante On Click:
-(void) uploadImage
{
prodNam = txtProdName.text;
UIImage * img = [UIImage imageNamed:@"SRT2.jpg"];
NSData *imageData = UIImageJPEGRepresentation(img,0.2); //change Image to NSData
if (imageData != nil)
{
NSString * filenames = [NSString stringWithFormat:@"TextLabel"];
NSLog(@"%@", filenames);
NSString *urlString = @"http://dev9.edisbest.com/upload_image.php";
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]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"TestEdreamzIpad.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Response : %@",returnString);
if([returnString isEqualToString:@"Success ! The file has been uploaded"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Image Saved Successfully" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
NSLog(@"Finish");
}
}
Puoi metti qualsiasi immagine nella posizione di "SRT2.jpg" della tua gerarchia locale xcode.
Tutto il meglio ...
Ehi, puoi caricare anche il tuo PHP COde? Ho provato il tuo codice ma dice sempre "C'è stato un errore nel caricare il file, per favore riprova!" E non ci sono dati compaiono sul mio server ... –
@ LaurenzGlück: Codice Per:? upload_image.php Php $ percorso_destinazione = "iphonetest /"; $ target_path = $ target_path.$ _FILES [ 'userfile'] [ 'name']; $ nomefile = $ _FILES ['userfile'] ['name']; if (move_uploaded_file ($ _ FILES ['userfile'] ['tmp_name'], $ target_path)) { echo "1"; } altro { echo "0"; } ?> "iphonetest" è la directory del server inserita nella directory principale. Usa e rispondi è degno di te? – Pushkraj
@ LaurenzGlück: Php include ("include/connect.php"); include ("../ wp-config.php"); /** * percorso di caricamento dell'immagine per aggiungere il prodotto */ $ wp_upload_dir = wp_upload_dir(); $ target_path = $ wp_upload_dir ['percorso']. '/'; $ target_path = $ target_path. $ _ FILES ['userfile'] ['name']; $ nomefile = $ _FILES ['userfile'] ['name']; if (move_uploaded_file ($ _ FILES ['userfile'] ['tmp_name'], $ target_path)) { echo "1"; echo $ target_path; } altro { echo "0"; echo $ target_path; } ?> – Pushkraj
miei servizi web è questo uno e che sta funzionando bene!
<?php
$fp = fopen("./trace.txt", "w"); //creates a file to trace your data
fwrite($fp,"get \n");
fwrite($fp, print_r($_GET, true));
fwrite($fp,"POST \n");
fwrite($fp, print_r($_POST, true));//displays the POST
fwrite($fp,"FILES \n");
fwrite($fp,print_r($_FILES,true));//display the FILES
fclose($fp);
$myparam = $_POST["userfile"];
$mytextLabel = $_POST['filenames'];
echo $myparam;
echo $mytextLabel;
$uploadDir = 'uploads/'; //you must create this directory
$uploadDir = $uploadDir.basename($_FILES['myfile']['name']); //saves the picture inside that folder
$file = basename($_FILES['uploaded']['name']);
//$uploadedFile = $uploadDir.$file;
//move_uploaded_file($_FILES['uploaded']['tpm_name']);
if(move_uploaded_file($_FILES['myfile']['tpm_name'],$uploadDir)){
echo "the file ".basename($_FILES['myfile']['name'])." has been uploaded" ;
}else{
echo "error";
}
?>
Semplicemente Utente SVHttp Request per caricare file sul server.
invio imageFile e altri animali nel parametro:
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
[data setObject:imageData forKey:@"image"];
[data setObject:@"saveimage" forKey:@"action"];
[data setObject:userId forKey:@"userid"];
[data setObject:@"png" forKey:@"type"];
e di domandare:
SVHTTPClient *request = [SVHTTPClient sharedClient];
[request setBasicAuthWithUsername:nil password:nil];
[request setSendParametersAsJSON:NO];
[request POST:[NSString stringWithFormat:@"http://bylyngo.com/getapi/?action=saveimage&userid=%@&type=png",userId]
parameters:data
progress:^(float progress) {
NSLog(@"Uploading (%.0f%%)", progress*100);
}];
Otterrete i progressi ancora.
:)
- 1. Come inviare richiesta di richiesta HTTP POST al server
- 2. Come posso inviare dati di post e header con Chromium?
- 3. File e dati POST restangolari
- 4. firebug: come inviare i dati POST nella console di Firebug?
- 5. Come inviare dati di grandi dimensioni utilizzando il metodo POST?
- 6. PHP - Reindirizzare e inviare dati tramite POST
- 7. Ancora HTML per inviare i dati del post al clic
- 8. Come visualizzare l'immagine selezionata senza inviare dati al server?
- 9. per inviare un file di testo al server utilizzando javascript
- 10. Come posso vedere quali file Git sta per inviare al server in Xcode?
- 11. Come posso inviare dati POST e navigare con JQuery?
- 12. In che modo il server può inviare dati al client?
- 13. Immagine post Android sul server utilizzando MultipartEntity
- 14. Come inviare array utilizzando XMLHttpRequest al server
- 15. non è in grado di dividere un file e inviare e quindi partecipare al server
- 16. come inviare file audio dal client al server Android servlet
- 17. come inviare un'immagine al server Web
- 18. Come passare i dati GET e POST all'eseguibile di php?
- 19. come inviare i dati multipart/form al server web da Android?
- 20. jQuery UI completamento automatico: come inviare i dati del post?
- 21. jqGrid: dati POST al server per recuperare i dati delle righe (filtraggio e ricerca)
- 22. Come posso inviare file di grandi dimensioni al server da Android con AndroidAnnotation?
- 23. Caricamento dati immagine 'canvas' sul server
- 24. Come posso inviare un file in una richiesta POST?
- 25. MultipartEntityBuilder per inviare immagini al server ferroviario
- 26. SQL Server varbinary (max) a Immagine tipo di dati
- 27. Generazione di dati immagine HTML Canvas lato server?
- 28. Invia file immagine utilizzando le connessioni POST HTTP java
- 29. Come inviare un dati nidificati e un elenco di immagini
- 30. Perché Jquery invia "undefined = undefined" come parametri del mio post invece di inviare i dati dell'array?
Quale servizio è questo? –
Obiettivo c, Questa è una domanda simile, ma ho bisogno di un testo. – sebi
http://stackoverflow.com/questions/125306/how-can-i-upload-a-photo-to-a-server-with-the-iphone – sebi