我通过POST将文件传送到php脚本时出现问题。
我想模拟一个使用multipart / form-data的表单,将POST数据发送到我的PHP脚本。
form.php的:
<form enctype="multipart/form-data" action="csvimport.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000">
CSV-File: <input name="userfile" type="file">
Database: <select name="db">
<option value="db1" selected>Db1</option>
<option value="db2">Db2</option>
<option value="db3">Db3</option>
</select>
Passkey: <input name="passkey" type="password">
<input type="submit" value="Send File" class="UploadFormSubmitButton"></form>
我必须阅读这些变量中的数据:
$_POST["passkey"] $_POST["db"] $_POST["MAX_FILE_SIZE"] $_FILES['userfile']['name']
我正在使用此代码:
public static void HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream rs = wr.GetRequestStream();
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, key, nvc[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
rs.Write(formitembytes, 0, formitembytes.Length);
}
rs.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string header = string.Format(headerTemplate, paramName, file, contentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
WebResponse wresp = null;
try
{
wresp = wr.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
MessageBox.Show(reader2.ReadToEnd()); //***!!SAYS UPLOAD FAILS!!***
}
catch (Exception ex)
{
//log.Error("Error uploading file", ex);
if (wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
wr = null;
}
}
并调用函数:
Database db = new Database();
NameValueCollection nvc = new NameValueCollection();
nvc.Add("passkey", "123");
nvc.Add("db", "db1");
nvc.Add("MAX_FILE_SIZE", "30000");
db.sendCSV("http://.../csvimport.php", "C:\\Users\\Ivo\\Documents\\FILEtoUPLOAD.csv", "file", "text/comma-separated-values", nvc);
问题代码:
$uploadfile = basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{ echo "UPLOAD SUCCESFULL"; }
else { echo "UPLOAD FAIL"; }
- &GT;我可以阅读所有的帖子变量,但是“$ _FILES ['userfile'] ['name']”&amp; “$ _FILES ['userfile'] ['tmp_name']”在我使用Web请求时是空的!
当我在Html表单上传文件时,我有类似“FILEtoUPLOAD.csv”和路径的内容..任何想法如何解决这个问题?