Mat*_*att 1 asp.net-mvc file-io controller http-post actionscript-3
这是我的方法
[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);
}
Run Code Online (Sandbox Code Playgroud)
它将从此actionscript方法发布到:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
但是当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;"/>
<%} %>
Run Code Online (Sandbox Code Playgroud)
控制器:
var file = Request.Files["file"];
byte[] buf = new byte[file.ContentLength];
file.InputStream.Read(buf, 0, file.ContentLength);
Run Code Online (Sandbox Code Playgroud)
我不确定这是否是您正在寻找的,但就像我说我有很多问题允许我的用户在我的网站上传他们自己的照片.
基本上我有一个View,其中包含一个类型为"file"的字段,以及一个回复事件的"Submit"按钮.
然后在控制器中我有以下内容;
[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);
Run Code Online (Sandbox Code Playgroud)
我遇到的困难是让控制器获取请求以及文件名和路径.一旦我有了,我可以将文件转换为一个字节数组,然后我可以保存到SQL数据库和图像字段.
我遗漏的最重要的事情是View中需要阅读的Form方法
<% using (Html.BeginForm( "PhotoAdd", "Photos", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
Run Code Online (Sandbox Code Playgroud)
enctype允许您获取所选文件的文件名和路径,并使控制器能够获取输入流.没有它我发现Request.Files是空的或它只包含文件名,因此无法打开它.
将图像保存到数据库后,显示它是一件轻而易举的事.
我希望这回答了你的问题.
| 归档时间: |
|
| 查看次数: |
2848 次 |
| 最近记录: |