有麻烦调用控制器Post方法
-
20-08-2019 - |
题
这是我的方法
[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 在地址栏中提示“没有找到您要查的资源”。
任何人有任何想法,为什么它会做这样或我可以做什么去揣摩吧?
如果您需要任何更多信息,请让我知道,我会更新我的问题。
谢谢,结果 马特
解决方案
我有麻烦[很多]与此,我到底采用这种;
查看强>:
<% 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" })) { %>
在是enctype允许你拾取所选择的文件的文件名和路径和控制器能够抓住InputStream的。没有它,我发现,无论是Request.Files是空的,或者它仅包含文件名,因此不能打开它。
一旦你将图像保存到数据库,显示它是不费吹灰之力。
我希望这回答了你的问题。
不隶属于 StackOverflow