2 gawkingdotnet520 gawkingdotnet520 于 2013.12.26 14:40 提问

C# flash 图片上传出现 “文件上传过程中出现I/O错误”

前台WebForm1.aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Web.User.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<br> function $(id) {<br> return document.getElementById(id);<br> }<br> var userAgent = navigator.userAgent.toLowerCase();<br> var is_opera = userAgent.indexOf(&#39;opera&#39;) != -1 &amp;&amp; opera.version();<br> var is_moz = (navigator.product == &#39;Gecko&#39;) &amp;&amp; userAgent.substr(userAgent.indexOf(&#39;firefox&#39;) + 8, 3);<br> var is_ie = (userAgent.indexOf(&#39;msie&#39;) != -1 &amp;&amp; !is_opera) &amp;&amp; userAgent.substr(userAgent.indexOf(&#39;msie&#39;) + 5, 3);<br> var is_mac = userAgent.indexOf(&#39;mac&#39;) != -1;<br> function AC_GetArgs(args, classid, mimeType) {<br> var ret = new Object();<br> ret.embedAttrs = new Object();<br> ret.params = new Object();<br> ret.objAttrs = new Object();<br> for (var i = 0; i &lt; args.length; i = i + 2) {<br> var currArg = args[i].toLowerCase();<br> switch (currArg) {<br> case &quot;classid&quot;: break;<br> case &quot;pluginspage&quot;: ret.embedAttrs[args[i]] = &#39;<a href="http://www.macromedia.com/go/getflashplayer">http://www.macromedia.com/go/getflashplayer</a>&#39;; break;<br> case &quot;src&quot;: ret.embedAttrs[args[i]] = args[i + 1]; ret.params[&quot;movie&quot;] = args[i + 1]; break;<br> case &quot;codebase&quot;: ret.objAttrs[args[i]] = &#39;<a href="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0">http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0</a>&#39;; break;<br> case &quot;onafterupdate&quot;: case &quot;onbeforeupdate&quot;: case &quot;onblur&quot;: case &quot;oncellchange&quot;: case &quot;onclick&quot;: case &quot;ondblclick&quot;: case &quot;ondrag&quot;: case &quot;ondragend&quot;:<br> case &quot;ondragenter&quot;: case &quot;ondragleave&quot;: case &quot;ondragover&quot;: case &quot;ondrop&quot;: case &quot;onfinish&quot;: case &quot;onfocus&quot;: case &quot;onhelp&quot;: case &quot;onmousedown&quot;:<br> case &quot;onmouseup&quot;: case &quot;onmouseover&quot;: case &quot;onmousemove&quot;: case &quot;onmouseout&quot;: case &quot;onkeypress&quot;: case &quot;onkeydown&quot;: case &quot;onkeyup&quot;: case &quot;onload&quot;:<br> case &quot;onlosecapture&quot;: case &quot;onpropertychange&quot;: case &quot;onreadystatechange&quot;: case &quot;onrowsdelete&quot;: case &quot;onrowenter&quot;: case &quot;onrowexit&quot;: case &quot;onrowsinserted&quot;: case &quot;onstart&quot;:<br> case &quot;onscroll&quot;: case &quot;onbeforeeditfocus&quot;: case &quot;onactivate&quot;: case &quot;onbeforedeactivate&quot;: case &quot;ondeactivate&quot;: case &quot;type&quot;:<br> case &quot;id&quot;: ret.objAttrs[args[i]] = args[i + 1]; break;<br> case &quot;width&quot;: case &quot;height&quot;: case &quot;align&quot;: case &quot;vspace&quot;: case &quot;hspace&quot;: case &quot;class&quot;: case &quot;title&quot;: case &quot;accesskey&quot;: case &quot;name&quot;:<br> case &quot;tabindex&quot;: ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i + 1]; break;<br> default: ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i + 1];<br> }<br> }<br> ret.objAttrs[&quot;classid&quot;] = classid;<br> if (mimeType) {<br> ret.embedAttrs[&quot;type&quot;] = mimeType;<br> }<br> return ret;<br> }<br> function AC_FL_RunContent() {<br> var ret = AC_GetArgs(arguments, &quot;clsid:d27cdb6e-ae6d-11cf-96b8-444553540000&quot;, &quot;application/x-shockwave-flash&quot;);<br> var str = &#39;&#39;;<br> if (is_ie &amp;&amp; !is_opera) {<br> str += &#39;<object ';<br> for (var i in ret.objAttrs) {<br> str += i + '="' + ret.objAttrs[i] + '" ';<br> }<br> str += '>&#39;;<br> for (var i in ret.params) {<br> str += &#39;<param name="' + i + '" value="' + ret.params[i] + '" /> &#39;;<br> }<br> str += &#39;</object>&#39;;<br> } else {<br> str += &#39;<embed ';<br> for (var i in ret.embedAttrs) {<br> str += i + '="' + ret.embedAttrs[i] + '" ';<br> }<br> str += '></embed>&#39;;<br> }<br> return str;<br> }</p> <pre><code>&lt;/script&gt; </code></pre> <p></head><br> <body><br> <form id="form1" runat="server"><br> <table cellspacing="0" cellpadding="0" ><br> <tbody><br> <tr><br> <th></th><br> <td><br><br> <div class="avatararea"><br> <p><img id="avatar" onerror="this.onerror=null;this.src='../image/head.png';" /></p><br> <p><a href="javascript:;" onclick="$('avatarctrl').style.display = ''">正面</a><br> </p><br> </div><br> <div id="avatarctrl" style="display: none"><br> <script type="text/javascript"><br> document.write(AC_FL_RunContent(&#39;width&#39;, &#39;540&#39;, &#39;height&#39;, &#39;253&#39;, &#39;scale&#39;, &#39;exactfit&#39;, &#39;src&#39;, &#39;&lt;% =avatarFlashParam %&gt;&#39;, &#39;id&#39;, &#39;mycamera&#39;, &#39;name&#39;, &#39;mycamera&#39;, &#39;quality&#39;, &#39;high&#39;, &#39;bgcolor&#39;, &#39;#ffffff&#39;, &#39;wmode&#39;, &#39;transparent&#39;, &#39;menu&#39;, &#39;false&#39;, &#39;swLiveConnect&#39;, &#39;true&#39;, &#39;allowScriptAccess&#39;, &#39;always&#39;));<br>

<br> function updateavatar(sender, args) {<br> $(&#39;avatar&#39;).src = &#39;&lt;%=Localhost %&gt;/image/upload/idcardphoto/&lt;%=uid %&gt;/medium.jpg?random=1&#39; + Math.random();<br> $(&#39;avatarctrl&#39;).style.display = &#39;none&#39;;<br> }<br> updateavatar();<br>








后台WebForm1.aspx.cs页面:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web.User
{
public partial class WebForm1 : System.Web.UI.Page
{
protected string avatarFlashParam;
protected string EncodeLocalhost;
protected string Localhost;
protected string uid;
protected void Page_Load(object sender, EventArgs e)
{
int port = HttpContext.Current.Request.Url.Port;
string ApplicationPath = HttpContext.Current.Request.ApplicationPath != "/" ? HttpContext.Current.Request.ApplicationPath : string.Empty;
uid = "1";
Localhost = string.Format("{0}://{1}{2}{3}",
HttpContext.Current.Request.Url.Scheme,
HttpContext.Current.Request.Url.Host,
(port == 80 || port == 0) ? "" : ":" + port,
ApplicationPath);
EncodeLocalhost = HttpUtility.UrlEncode(Localhost);
avatarFlashParam = string.Format("{0}/image/common/camera.swf?nt=1&inajax=1&appid=1&input={1}&ucapi={2}/PhotoAjax.ashx", Localhost, uid, EncodeLocalhost);
}
}
}

PhotoAjax页面:
<%@ WebHandler Language="C#" Class="PhotoAjax" %>

using System;
using System.Web;
using System.IO;

public class PhotoAjax : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
    string uid = context.Request.QueryString["input"];
    if (!string.IsNullOrEmpty(context.Request["Filename"]) && !string.IsNullOrEmpty(context.Request["Upload"]))
    {
        ResponseText(UploadTempAvatar(uid));
    }
    if (!string.IsNullOrEmpty(context.Request["avatar1"]) && !string.IsNullOrEmpty(context.Request["avatar2"]) && !string.IsNullOrEmpty(context.Request["avatar3"]))
    {
        CreateDir(uid);
        if (!(SaveAvatar("avatar1", uid) && SaveAvatar("avatar2", uid) && SaveAvatar("avatar3", uid)))
        {
            File.Delete(GetMapPath("..\\image\\upload\\idcardphoto\\" + uid + ".jpg"));
            ResponseText("<?xml version=\"1.0\" ?><root><face success=\"0\"/></root>");
            return;
        }
        File.Delete(GetMapPath("..\\image\\upload\\idcardphoto\\" + uid + ".jpg"));
        ResponseText("<?xml version=\"1.0\" ?><root><face success=\"1\"/></root>");
        return;
    }
}

public bool IsReusable
{
    get
    {
        return false;
    }
}

private void CreateDir(string uid)
{
    string avatarDir = string.Format("../image/upload/idcardphoto/{0}",
         uid);
    if (!Directory.Exists(GetMapPath(avatarDir)))
        Directory.CreateDirectory(GetMapPath(avatarDir));
}

private void ResponseText(string text)
{
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Write(text);
    HttpContext.Current.Response.End();
}

private string UploadTempAvatar(string uid)
{
    string filename = uid + ".jpg";
    string uploadUrl = GetRootUrl("../image/") + "upload/idcardphoto";
    string uploadDir = GetMapPath("..\\image\\upload\\idcardphoto");
    if (!Directory.Exists(uploadDir + "temp\\"))
        Directory.CreateDirectory(uploadDir + "temp\\");

    filename = "temp/" + filename;
    if (HttpContext.Current.Request.Files.Count > 0)
    {
        HttpContext.Current.Request.Files[0].SaveAs(uploadDir + filename);
    }

    return uploadUrl + filename;
}

private byte[] FlashDataDecode(string s)
{
    byte[] r = new byte[s.Length / 2];
    int l = s.Length;
    for (int i = 0; i < l; i = i + 2)
    {
        int k1 = ((int)s[i]) - 48;
        k1 -= k1 > 9 ? 7 : 0;
        int k2 = ((int)s[i + 1]) - 48;
        k2 -= k2 > 9 ? 7 : 0;
        r[i / 2] = (byte)(k1 << 4 | k2);
    }
    return r;
}

private bool SaveAvatar(string avatar, string uid)
{
    byte[] b = FlashDataDecode(HttpContext.Current.Request[avatar]);
    if (b.Length == 0)
        return false;
    string size = "";
    if (avatar == "avatar1")
        size = "large";
    else if (avatar == "avatar2")
        size = "medium";
    else
        size = "small";
    string avatarFileName = string.Format("../image/upload/idcardphoto/{0}/{1}.jpg",
        uid, size);
    FileStream fs = new FileStream(GetMapPath(avatarFileName), FileMode.Create);
    fs.Write(b, 0, b.Length);
    fs.Close();
    return true;
}

public static string GetRootUrl(string forumPath)
{
    string ApplicationPath = HttpContext.Current.Request.ApplicationPath != "/" ? HttpContext.Current.Request.ApplicationPath : string.Empty;
    int port = HttpContext.Current.Request.Url.Port;
    return string.Format("{0}://{1}{2}{3}/{4}",
                         HttpContext.Current.Request.Url.Scheme,
                         HttpContext.Current.Request.Url.Host,
                         (port == 80 || port == 0) ? "" : ":" + port,
                         ApplicationPath,
                         forumPath);
}

public static string GetMapPath(string strPath)
{
    if (HttpContext.Current != null)
    {
        return HttpContext.Current.Server.MapPath(strPath);
    }
    else //非web程序引用
    {
        strPath = strPath.Replace("/", "\\");
        if (strPath.StartsWith("\\"))
        {
            strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
        }
        return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
    }
}

}

我在网上找的一个flash图片上传,我拿到自己的程序里之后上传图片就提示如下错误,找了半天没找到原因:
CSDN移动问答

1个回答

szjdz
szjdz   2013.12.26 16:48

具体到哪一步出错?

Csdn user default icon
上传中...
上传图片
插入图片