【问题标题】:Sending Image by using callback to server by javascript通过javascript使用回调将图像发送到服务器
【发布时间】:2013-01-08 08:31:12
【问题描述】:

当我尝试执行代码时,我试图通过回调方法将图像保存在服务器上,但出现异常演示代码示例:http://catalog.codeproject.com/Articles/17193/Upload-Images-Using-C-JavaScript-and-ASP-NET-2-0-C

流 s = File.OpenRead(returnValue);在cs文件中我得到异常文件未找到

  <input id="File1" runat="server"  onchange="PopulateList(this)" name="File1" type="File" />

<script language="javascript" type="text/javascript">

    var counter = 1;
    var newPath = '';
    var filePath = '';


    function PopulateList(obj) {
        // Upload the image to the server folder 
        filePath = obj.value;
        // calls the server's method using client callbacks    
        CallServer(obj.value, '');
    }

    function ReceiveServerData(rValue) {

        // The new path will contain the path of the image which is inside the server's folder 
        newPath = rValue;
        alert(newPath);
        //CreateNestedElements();

    }
</script>

在 Cs 文件中

protected string returnValue = String.Empty;

protected void Page_Load(object sender, EventArgs e)
{
    string[] filePaths = null;

    //HtmlInputHidden hiddenControl = (HtmlInputHidden)Page.FindControl("list");
    //if (!String.IsNullOrEmpty(hiddenControl.Value))
    //{
    //    filePaths = hiddenControl.Value.Split('|');

    //    //SaveFilesToDB(filePaths);

    //}

    // register the callback script 

    string sbReference = ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");

    string cbScript = String.Empty;

    // check if the script is already registered or not 

    if (!ClientScript.IsClientScriptBlockRegistered("CallServer"))
    {

        cbScript = @" function CallServer(arg,context) { " + sbReference + "}";

        ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", cbScript, true);

    }
}


public string GetCallbackResult()
{
    string fileName = System.IO.Path.GetFileName(returnValue);

    string path = Server.MapPath("Images/");
    string fullPath = path + fileName;

    Stream s = File.OpenRead(returnValue);

    byte[] buffer = new byte[s.Length];
    s.Read(buffer, 0, (int)s.Length);

    int len = (int)s.Length;

    s.Dispose();
    s.Close();

    FileStream fs = new FileStream(fullPath, FileMode.Create);
    fs.Write(buffer, 0, len);

    Bitmap bmp = new Bitmap(fs);


    if (System.IO.Path.GetExtension(returnValue).Equals(".gif"))
    {
        bmp.Save(fs, ImageFormat.Gif);
    }
    else
    {
        bmp.Save(fs, ImageFormat.Jpeg);
    }

    bmp.Dispose();

    fs.Dispose();
    fs.Close();                      

    return "Images/" + fileName;
}

public void RaiseCallbackEvent(string eventArgument)
{
    if (!String.IsNullOrEmpty(eventArgument))
    {
        returnValue = eventArgument;
    }
}

【问题讨论】:

    标签: c# asp.net callback


    【解决方案1】:

    不幸的是,您所链接的code-project article 完全是垃圾——代码只有在您使用与客户端和服务器相同的机器时才能工作,而且也只能在 Internet Explorer 中工作。在其他情况下,您一定会遇到异常,因为已发送到服务器的文件路径将是客户端计算机上的本地文件路径,并且不会出现在服务器计算机上(导致 File.OpenRead 失败,因为路径无效在服务器上)。

    我不确定你想要达到的效果——如果你想异步上传文件,你会发现很多插件或示例。比如你可以使用AJAX工具包AsyncFileUpload控件。

    【讨论】:

      猜你喜欢
      • 2013-03-27
      • 2020-04-16
      • 2014-05-10
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多