Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

upload fichier android vers formulaire http

Featured Replies

Posté(e)

Bonjour,

je cherche à uploader un fichier image depuis android vers un formulaire http. Après de longues heures cela ne fonctionne pas, et je ne vois pas où est le problème.

Pourriez-vous m'aider s'il vous plait ? voici l'extrait de code.

public void doHttpUpload(File myImage) {
	int check = 0;

	String lineEnd = "\r\n";
	String twoHyphens = "--";
	String boundary = "*****";
	String photofile = null;
	String httpResponse; //to read http response
	String filename=null;

	String urlString = "http://dredge.phpnet.org/Test/form_upload.html";
	HttpURLConnection conn = null;

	InputStream fis = null;
	Bitmap mBitmap=null;
	String pathfile;

	filename = "myFile.png";

	try {
		URL site = new URL(urlString);
		conn = (HttpURLConnection) site.openConnection();

		//on peut écrire et lire
		conn.setDoOutput(true);
		conn.setDoInput(true);

		// Use a post method.
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Connection", "Keep-Alive");
		conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
		//Log.d("taille image",""+myImage.length());
		fis = new FileInputStream(myImage);
		InputStream in = null;
		in = new BufferedInputStream(fis);

		//mBitmap = BitmapFactory.decodeStream(fis);

		DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

		dos.writeBytes("Content-Disposition: form-data;myFile=\"" + filename + "\"" + lineEnd);
		dos.writeBytes("Content-Type: image/gif" +lineEnd +lineEnd);


		// send multipart form data necesssary after file data...
		int bytesAvailable = in.available();//fis.available();
		Log.d("bytesAvailable",""+bytesAvailable);
		int maxBufferSize = 1*1024*1024;
	    int bufferSize = Math.min(bytesAvailable, maxBufferSize);
	    byte[] buffer = new byte[bufferSize];

	       // read file and write it into form...
	       int bytesRead = in.read(buffer, 0, bufferSize);
	       Log.d("nb byteread",""+bytesRead);
	       while (bytesRead > 0){
	    	   dos.write(buffer, 0, bufferSize);
	    	   bytesAvailable = in.available();
	    	   Log.d("nb bytesAvailable",""+bytesAvailable);
	    	   bufferSize = Math.min(bytesAvailable, maxBufferSize);
	    	   bytesRead = in.read(buffer, 0, bufferSize);
	       }

		//dos.writeBytes(lineEnd);
		//dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

		// close streams
		fis.close();
		dos.flush();
		dos.close();
		Log.e("fileUpload","File is written on the queue");

		} catch (MalformedURLException e) {
		e.printStackTrace();
		//Toast.makeText(HttpUploader.this, "échec de connexion au site web ", Toast.LENGTH_SHORT).show();
		Log.i(getClass().getSimpleName(),"échec de connexion au site web 1");
		} catch (IOException e) {
		e.printStackTrace();
		//Toast.makeText(HttpUploader.this, "échec de connexion au site web ", Toast.LENGTH_SHORT).show();
		Log.i(getClass().getSimpleName(),"échec de connexion au site web 2");
	}

merci de votre aide :emo_im_angel:

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.