Feeds:
文章
留言

Archive for 九月 6th, 2006

.NET 提供的  WebClient.UploadFile() 讓程式開發者可以很容易達到 HTML 的 File Submit 功能,只需要下面幾行

 

using System;
using System.Net;
namespace UploadFileTest {
class Program {
    static void Main(string[] args) {
        WebClient wc = new WebClient(); 
        byte[] responseArray = wc.UploadFile("http://192.168.10.120:2527/uf.php?action=set_learn_record&file=xyz", "POST",         "c:/batch.php");
        Console.WriteLine("nResponse Received.The contents of the file uploaded are:n{0}",
        System.Text.Encoding.ASCII.GetString(responseArray));
        Console.ReadLine(); 
    }
  }
}

伺服端我用 PHP 接收如下:  

<?php
var_dump($_GET);
print_r($_POST);
var_dump($_FILES);

if (is_uploaded_file($_FILES[‘file’][‘tmp_name’])) {
       echo "File ". $_FILES[‘file’][‘name’] ." uploaded successfully.n"
       echo "Displaying contentsn"
       readfile($_FILES[‘file’][‘tmp_name’]);
} else {
       echo "Possible file upload attack: "
       echo "filename ‘". $_FILES[‘file’][‘tmp_name’] . "’."
}
?>

 

執行結果如下:

如果你有設定 App.Config 你會發現,其實 WebClient 內部是使用 HttpWebRequest 和 HttpWebREsponse 實作,下面是擷取上傳檔案過程的資訊  

System.Net Verbose: 0 : [4652] WebClient#28068188::UploadFile(http://192.168.10.120:2527/uf.php?action=set_learn_record&file=xyz, POST)
System.Net Verbose: 0 : [4652] WebRequest::Create(http://192.168.10.120:2527/uf.php?action=set_learn_record&file=xyz)
System.Net Verbose: 0 : [4652] HttpWebRequest#33163964::HttpWebRequest(http://192.168.10.120:2527/uf.php?action=set_learn_record&file=xyz#264857726)
System.Net Verbose: 0 : [4652] Exiting HttpWebRequest#33163964::HttpWebRequest()
System.Net Verbose: 0 : [4652] Exiting WebRequest::Create() -> HttpWebRequest#33163964
System.Net Verbose: 0 : [4652] HttpWebRequest#33163964::GetRequestStream()
System.Net Information: 0 : [4652] Associating HttpWebRequest#33163964 with ServicePoint#14421545
System.Net Information: 0 : [4652] Associating Connection#35567111 with HttpWebRequest#33163964
System.Net Information: 0 : [4652] Associating HttpWebRequest#33163964 with ConnectStream#65066874
System.Net Information: 0 : [4652] HttpWebRequest#33163964 – Request: POST /uf.php?action=set_learn_record&file=xyz HTTP/1.1System.Net Information: 0 : [4652] ConnectStream#65066874 – 正在傳送標頭
{
Content-Type: multipart/form-data; boundary=———————8c89fdbf5c65628
Host: 192.168.10.120:2527
Content-Length: 340
Expect: 100-continue
Connection: Keep-Alive
}。
System.Net Information: 0 : [4652] Connection#35567111 – 收到的狀態列: Version=1.1,StatusCode=100,StatusDescription=Continue。
System.Net Information: 0 : [4652] Connection#35567111 – 收到的標頭
{

}。

System.Net Verbose: 0 : [6136] Exiting HttpWebRequest#7588182::GetRequestStream() -> ConnectStream#50510248
System.Net Verbose: 0 : [6136] ConnectStream#50510248::Write()
System.Net Verbose: 0 : [6136] Data from ConnectStream#50510248::Write
System.Net Verbose: 0 : [6136] 00000000 : 2D 2D 2D 2D 2D 2D 2D 2D-2D 2D 2D 2D 2D 2D 2D 2D : —————-
System.Net Verbose: 0 : [6136] 00000010 : 2D 2D 2D 2D 2D 2D 2D 38-63 38 39 66 64 65 38 31 : ——-8c89fde81
System.Net Verbose: 0 : [6136] 00000020 : 32 64 64 33 64 38 0D 0A-43 6F 6E 74 65 6E 74 2D : 2dd3d8..Content-
System.Net Verbose: 0 : [6136] 00000030 : 44 69 73 70 6F 73 69 74-69 6F 6E 3A 20 66 6F 72 : Disposition: for
System.Net Verbose: 0 : [6136] 00000040 : 6D 2D 64 61 74 61 3B 20-6E 61 6D 65 3D 22 66 69 : m-data; name="fi
System.Net Verbose: 0 : [6136] 00000050 : 6C 65 22 3B 20 66 69 6C-65 6E 61 6D 65 3D 22 62 : le" filename="b
System.Net Verbose: 0 : [6136] 00000060 : 61 74 63 68 2E 70 68 70-22 0D 0A 43 6F 6E 74 65 : atch.php"..Conte
System.Net Verbose: 0 : [6136] 00000070 : 6E 74 2D 54 79 70 65 3A-20 61 70 70 6C 69 63 61 : nt-Type: applica
System.Net Verbose: 0 : [6136] 00000080 : 74 69 6F 6E 2F 6F 63 74-65 74 2D 73 74 72 65 61 : tion/octet-strea
System.Net Verbose: 0 : [6136] 00000090 : 6D 0D 0A 0D 0A : m….

… 略

System.Net Verbose: 0 : [4652] HttpWebRequest#33163964::GetResponse()
System.Net Information: 0 : [4652] Connection#35567111 – 收到的狀態列: Version=1.1,StatusCode=200,StatusDescription=OK。
System.Net Information: 0 : [4652] Connection#35567111 – 收到的標頭
{
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html
Date: Wed, 06 Sep 2006 06:54:54 GMT
Server: Apache
X-Powered-By: PHP/4.3.11
}。
System.Net Information: 0 : [4652] ConnectStream#34160229::ConnectStream(已緩衝 -1 個位元組。)
System.Net Information: 0 : [4652] Associating HttpWebRequest#33163964 with ConnectStream#34160229
System.Net Information: 0 : [4652] Associating HttpWebRequest#33163964 with HttpWebResponse#63238509
System.Net Verbose: 0 : [4652] Exiting HttpWebRequest#33163964::GetResponse() -> HttpWebResponse#63238509
System.Net Verbose: 0 : [4652] HttpWebResponse#63238509::GetResponseStream()
… 略

 

這個方法雖然簡單,但是有一個缺點,就是一次只能上傳一個檔案,如果要上傳多個檔案似乎需要自行重新包裝一個類別。

另外,需要注意 WebClient 的 UploadFile 上傳的檔案預設是放在 file 這個參數中。

Read Full Post »

好厲害的 MS Access ……

今天在 MS Access 中下了一個更新查詢如下,結果居然跳出一個視窗要我輸入 Scorm_Type 的值

UPDATE cmi_core SET
       Scorm_Type = ‘sco’
WHERE
Course_ID = 10000672
AND SCO_ID = ‘LO01’
AND User_ID = ‘Mary’

我納悶啊,不是已經 assign 給你了還跟我要,且欄位名稱也不是保留字啊,怪,於是想說加上 [] 試試看

UPDATE cmi_core SET
       [Scorm_Type] = ‘sco’
WHERE
Course_ID = 10000672
AND SCO_ID = ‘LO01’
AND User_ID = ‘Mary’

居然給了我一個"UPDATE 陳述式的語法錯誤" 暈 ~ @_@

jeff 給了我建議用內建的查詢產生器試試看,結果他給了我下面的語法 (只是為了測試欄位)
UPDATE cmi_core SET cmi_core.Scorm_Type = "1"

怪了,他的居然可以執行,各位知道為什麼了嗎?

ㄏㄏ…. 原來我的 SQL 是由C# 產生,我由於撰碼上習慣縮排好看,都會使用 Tab 字元,想不到 MS Access 把他當作欄位名稱的一部分 @_@

真是我看瞎了眼也不知道問題出在哪….. 真是一個很好的經驗~~~~

Read Full Post »