2009-06-28 53 views
1

這裏是我的方法有麻煩調用控制器Post方法

[AcceptVerbs(HttpVerbs.Post)] 
    public void SaveImage(FormCollection formValues) 
    { 
     byte[] contents = Convert.FromBase64String(Request.Form["file"]); 
     System.IO.File.WriteAllBytes(Server.MapPath(Request.Form["name"]), contents); 
    } 

是越來越張貼到這個動作方法:

public function encodeAndSave(e:MouseEvent = null):void 
    { 
     var date:Date = new Date(); 
     var by:ByteArray = PNGEnc.encode(canvas.main_bdata); 
     var req:URLRequest = new URLRequest(server_path+"Home/SaveImage"); 
     var params:URLVariables = new URLVariables(); 
     params.file = Base64.encodeByteArray(by); 
     params.name = "MyImage.png"; 
     req.method = URLRequestMethod.POST; 
     req.data = params; 
     var ldr:URLLoader = new URLLoader(req); 

     ldr.addEventListener(Event.COMPLETE, complete); 

     ldr.load(req); 

     function complete(e:Event):void 
     { 
      navigateToURL(new URLRequest("?" + Math.random()), "_self"); 

     } 

    } 

但encodeAndSave方式運行時,沒有文件被保存到服務器...

有誰知道如何判斷SaveImage方法是否已經運行?另外,當我在地址欄中輸入: http://www.mysite.com/Home/SaveImage時,它會顯示「無法找到資源」。

任何人有任何想法,爲什麼它會這樣做,或者我能做些什麼來試圖找出它?

如果您需要更多信息,請讓我知道,我會更新我的問題。

謝謝
馬特

回答

7

我有一個麻煩[大量]與此,我到底該使用;

查看

<% using (Html.BeginForm("PhotoAdd", "Photos", FormMethod.Post, new { enctype = "multipart/form-data" })) { %> 

<input type="file" id="file" name="file" class="field" style="width:300px;"/> 

<%} %> 

控制器

var file = Request.Files["file"]; 
byte[] buf = new byte[file.ContentLength]; 
file.InputStream.Read(buf, 0, file.ContentLength); 

我不知道這是否是你要找的,但就像我說我有很多的問題,允許我的用戶在我的網站上傳自己的照片。

基本上我有一個視圖,它包含一個「文件」類型的字段和一個「提交」按鈕,用於回發事件。

然後在控制器中,我有以下;

[AcceptVerbs(HttpVerbs.Post), Authorize] 
    public ActionResult PhotoAdd(FormCollection collection) 
    { 
     try 
     { 
      var file = Request.Files["file"]; 

      byte[] buf = new byte[file.ContentLength]; 
      file.InputStream.Read(buf, 0, file.ContentLength); 

我碰到的困難是讓控制器獲取請求以及文件名和路徑。一旦我有了,我可以將文件轉換爲字節數組,然後我可以將其保存到SQL數據庫和圖像字段。

我缺少的是在需要讀取

<% using (Html.BeginForm("PhotoAdd", "Photos", FormMethod.Post, new { enctype = "multipart/form-data" })) { %> 

的加密類型,您可以拿起所選文件的文件名和路徑視圖的形式方法和控制器是一個最重要的事情能夠抓住輸入流。沒有它,我發現Request.Files是空的,或者它只包含文件名,因此無法打開它。

將圖像保存到數據庫後,顯示它是一個輕而易舉的事情。

我希望這能回答你的問題。

+0

你能解釋一下嗎?對不起,它看起來不像是一個即插即用的東西,所以我只想弄清楚它是如何工作的,所以我可以改變它有點爲我的網絡應用 – Matt 2009-06-29 03:56:57