Hi all
using the following code i am able to upload a file to server but
along with this i also want to send a parameter called id to the php
script ,,,,can any body help me
Here is the android code...
private void doFileUpload(String ExsistingFileName)
{
HttpURLConnection conn = null;
DataOutputStream DOS = null;
String LineEnd = "\r\n";
String TwoHyphens = "--";
String Boundary = "*****";
int BytesRead, BytesAvailable, BufferSize;
byte[] Buffer;
int MaxBufferSize = 1*1024*1024;
String UploadUrl = "http://www.netschoolzone.com/Android/
upload.php";
try
{
Log.e("FileUpload","Start");
FileInputStream FIS = new FileInputStream(new
File(ExsistingFileName) );
URL url = new URL(UploadUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-
data;boundary="+Boundary);
DOS = new DataOutputStream( conn.getOutputStream() );
DOS.writeBytes(TwoHyphens + Boundary + LineEnd);
DOS.writeBytes("Content-Disposition: form-data; name=
\"uploadedfile\";filename=\"" + ExsistingFileName +"\"" + LineEnd);
DOS.writeBytes(LineEnd);
Log.e("FileUpload","After WriteByte");
BytesAvailable = FIS.available();
BufferSize = Math.min(BytesAvailable, MaxBufferSize);
Buffer = new byte[BufferSize];
BytesRead = FIS.read(Buffer, 0, BufferSize);
while (BytesRead > 0)
{
DOS.write(Buffer, 0, BufferSize);
BytesAvailable = FIS.available();
BufferSize = Math.min(BytesAvailable, MaxBufferSize);
BytesRead = FIS.read(Buffer, 0, BufferSize);
}
DOS.writeBytes(LineEnd);
DOS.writeBytes(TwoHyphens + Boundary + TwoHyphens + LineEnd);
FIS.close();
DOS.flush();
BufferedReader Rd = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String Line;
while ((Line = Rd.readLine()) != null)
{
Toast.makeText(getBaseContext(),"OK",Toast.LENGTH_LONG).show();
}
DOS.close();
Rd.close();
}
catch (MalformedURLException ex)
{
Log.e("File Upload", "Error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
Log.e("File Upload", "Error: " + ioe.getMessage(), ioe);
}
}
Php code:
<?php
$today = getdate();
$wday = $today['mday'];
$month = $today['mon'];
$year = $today['year'];
$hours = $today['hours'];
$minutes = $today['minutes'];
$seconds = $today['seconds'];
// this is gmt time regardless of what the server is set to..
$postedtime = $year . "-" . $month . "-" . $wday . " " . $hours .
":" . $minutes . ":" . $seconds;
$timestamp = strtotime($postedtime);
if(count($_FILES)>0)
{
$target_path = "uploads/";
foreach($_FILES as $file)
{
$target_path = $target_path . $timestamp . "-" .
basename($file['name']);
if(move_uploaded_file($file['tmp_name'], $target_path))
{
chmod($target_path, 0755);
echo "The file ". basename($file['name'])." has
been uploaded<br /
>";
}
else
{
echo "There was an error uploading the file,
please try again!<br /
>";
}
}
}
echo "file name is :".$file['name']."\n";
echo "file completed";
die();
?>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en