无法从服务器Silverlight(3.0)下载文件?

时间:2012-06-08 16:08:40

标签: c# silverlight silverlight-3.0 webclient

我在下载文件时遇到问题。我在网上找不到任何下降的例子,所以在坏的和我的上传代码之间(有效)我​​有你在下面看到的内容。它根本不是处理器,我知道Uri不是问题所以我在这里。谢谢你的帮助。

主要代码

    private void btnDownload_Click(object sender, EventArgs e)
    {
        SaveFileDialog sfd = new SaveFileDialog();

        if((bool)sfd.ShowDialog())
        {
                string FileName = "test.xlsx";

                WebClient wc = new WebClient();
                wc.OpenReadCompleted += (s, ea) =>
                    {
                        try
                        {
                            using (Stream fs = (Stream)sfd.OpenFile())
                            {
                                int length = Convert.ToInt32(ea.Result.Length);
                                byte[] byteresult = new byte[length];

                                ea.Result.Write(byteresult, 0, length);
                                ea.Result.Close();
                                fs.Flush();
                                fs.Close();
                            };
                        }
                        catch { }
                    };
                wc.OpenReadAsync(new Uri("http://localhost:64168/FileHandler.ashx?filename=" + FileName + "&type=D"));
        }
    }

HttpHandler的

public void ProcessRequest(HttpContext Context)
{        
    string filename = Context.Request.QueryString["filename"].ToString();
    string filepath = Context.Server.MapPath(_ServerPath + filename);

    switch (Context.Request.QueryString["type"])
    {
        case "G": // Get
            GetFile(Context);
            break;
        case "U": // Upload
            UploadFile(Context.Request.InputStream, File.Create(filepath));
            break;
        case "D": // Download
            DownloadFile(filename, filepath, Context.Response);
            break;
    }
}

private void DownloadFile(string filename, string filepath, HttpResponse response)
{
    response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
    response.WriteFile(filepath);
}

0 个答案:

没有答案