上传文件部分protected void UploadFilesThread() { WebClient UploadObj = new WebClient(); object[] itemsCache = new object[fileListBox.Items.Count]; // Error out if we're asked to upload files that don't exist. if (0 == fileListBox.Items.Count) { ThrowWebException("You must browse for files before attempting an upload."); return; }// !TODO: Grant the ability to upload all files batch. WebClient // doesn't support multiple files in the same upload txn, even // though the standard does, so we'll roll our own classes later // to handle this. new FileIOPermission(PermissionState.Unrestricted).Assert(); fileListBox.Items.CopyTo(itemsCache, 0); for (int i = 0; i < itemsCache.Length; i++) { // Trap any failures. try { string item = (string)itemsCache[i]; // cast once UploadObj.UploadFile(uploadURL, item); // Remove from the list individually, in case RemoveFile(item); UpdateFileSizeDescription(); } catch (WebException e) { ThrowWebException("Could not upload all of the files; reason: " + e.Message); return; } } CodeAccessPermission.RevertAssert();// We can't call a COM event from a thread other than the one on // which the sinking occurred, or we'll generate an obscure // exception. So set a timer in the main thread and force this // event to occur elsewhere. StartTimer(); btnRemove.Enabled = false; } -------------------------------------------------------------------------------- 接收文件部分。这个是写在aspx页页中private void Page_Load(object sender, System.EventArgs e) { HttpPostedFile hpf; // Assume the user is POSTing. If not, reject attempt to connect. if (!Request.HttpMethod.Equals("POST")) { Response.StatusCode = 405; // 405 == Method Not Allowed Response.End(); } for (int j = 0; j < Request.Files.Count; j++) { hpf = Request.Files.Get(j); hpf.SaveAs("C:\\temp\\" + hpf.FileName.Substring(hpf.FileName.LastIndexOf('\\') + 1)); } Response.StatusCode = 200; Response.End(); }注意。代码是正解的,在你改动时,可能会出错。一般是aspx页面的错误。 最在的一个区别就是,用页面到页面传文件 时上传的文件名是c:\adf\adsf\adf.txt 但用这种方法上传时得到的文件名是 adf.txt这种的,所以在处理时要注意
WebClient UploadObj = new WebClient();
object[] itemsCache = new object[fileListBox.Items.Count]; // Error out if we're asked to upload files that don't exist.
if (0 == fileListBox.Items.Count) {
ThrowWebException("You must browse for files before attempting an
upload.");
return;
}// !TODO: Grant the ability to upload all files batch. WebClient
// doesn't support multiple files in the same upload txn, even
// though the standard does, so we'll roll our own classes later
// to handle this.
new FileIOPermission(PermissionState.Unrestricted).Assert();
fileListBox.Items.CopyTo(itemsCache, 0);
for (int i = 0; i < itemsCache.Length; i++) {
// Trap any failures.
try {
string item = (string)itemsCache[i]; // cast once
UploadObj.UploadFile(uploadURL, item);
// Remove from the list individually, in case
RemoveFile(item);
UpdateFileSizeDescription();
} catch (WebException e) {
ThrowWebException("Could not upload all of the files; reason:
" + e.Message);
return;
}
}
CodeAccessPermission.RevertAssert();// We can't call a COM event from a thread other than the one on
// which the sinking occurred, or we'll generate an obscure
// exception. So set a timer in the main thread and force this
// event to occur elsewhere.
StartTimer();
btnRemove.Enabled = false;
}
--------------------------------------------------------------------------------
接收文件部分。这个是写在aspx页页中private void Page_Load(object sender, System.EventArgs e)
{
HttpPostedFile hpf; // Assume the user is POSTing. If not, reject attempt to connect.
if (!Request.HttpMethod.Equals("POST"))
{
Response.StatusCode = 405; // 405 == Method Not Allowed
Response.End();
} for (int j = 0; j < Request.Files.Count; j++)
{
hpf = Request.Files.Get(j);
hpf.SaveAs("C:\\temp\\" +
hpf.FileName.Substring(hpf.FileName.LastIndexOf('\\') + 1));
}
Response.StatusCode = 200;
Response.End();
}注意。代码是正解的,在你改动时,可能会出错。一般是aspx页面的错误。
最在的一个区别就是,用页面到页面传文件 时上传的文件名是c:\adf\adsf\adf.txt
但用这种方法上传时得到的文件名是 adf.txt这种的,所以在处理时要注意