=============================客户端========================================== <html> <head> <title>File Upload Form</title> </head> <body> This form allows you to upload a file to the server.<br> <form enctype="multipart/form-data" method="post" action="test.php"> Send this file: <input name="userfile" type="file" /><br /> <input type="submit" value="Send File" /> </form> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Example</title> </head> <body> <?php print "Received {$_FILES['userfile']['name']} - its size is {$_FILES['userfile']['size']}"; if ( move_uploaded_file($_FILES["userfile"]["tmp_name"], "C:\Windows\Temp\a.txt")) { print '<p> The file has been successfully uploaded </p>'; } else { switch ($_FILES['userfile'] ['error']) { case 1: print '<p> The file is bigger than this PHP installation allows</p>'; break; case 2: print '<p> The file is bigger than this form allows</p>'; break; case 3: print '<p> Only part of the file was uploaded</p>'; break; case 4: print '<p> No file was uploaded</p>'; break; } } print_r($_FILES); print '<p> Tsss </p>';
using System; using System.Web; using System.Collections.Specialized; using System.Text; using System.Net; using System.IO; namespace UploadFileEx { /// <summary> /// Summary description for Class1. /// </summary> class Class1 {
string postdata; postdata = "?"; if (querystring!=null) { foreach(string key in querystring.Keys) { postdata+= key +"=" + querystring.Get(key)+"&"; } } //Uri uri = new Uri(url+postdata); Uri uri = new Uri(url);
// Build the trailing boundary string as a byte array // ensuring the boundary appears on a line by itself byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
FileStream fileStream = new FileStream(uploadfile, FileMode.Open, FileAccess.Read); long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length; webrequest.ContentLength = length;
// Write out our post header requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
// Write out the file contents byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))]; int bytesRead = 0; while ( (bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0 ) requestStream.Write(buffer, 0, bytesRead); // Write out the trailing boundary requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); WebResponse responce = webrequest.GetResponse(); Stream s = responce.GetResponseStream(); StreamReader sr = new StreamReader(s);
return sr.ReadToEnd(); } /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { CookieContainer cookies = new CookieContainer(); //add or use cookies
NameValueCollection querystring = new NameValueCollection(); // simulate this form //<form action ="http://localhost/test.php" method = POST> //<input type = text name = uname> //<input type = password name =passwd> //<input type = FILE name = uploadfile> //<input type=submit>