原理:
先用过时的webclient下载文件到tmp目录,然后再读取文件加载显示
这里是前端
<dx:ASPxRichEdit ID="ASPxRichEdit1" runat="server" WorkDirectory="~\App_Data\WorkDirectory" Width="100%" Height="1920px" ReadOnly="True" ShowFormulaBar="False" FullscreenMode="True" SettingsView-Mode="Reading" RibbonMode="None">
</dx:ASPxRichEdit>
<dx:ASPxSpreadsheet runat="server" ID="excel" WorkDirectory="~/App_Data/WorkDirectory" Width="100%" ReadOnly="True" ShowFormulaBar="False" FullscreenMode="True" SettingsView-Mode="Reading" RibbonMode="None">
<SettingsView Mode="Reading" />
</dx:ASPxSpreadsheet>
这里是后端代码
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace docx
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//获取URL参数
string urlFile=Request.QueryString["filename"];
string id = Request.QueryString["id"];
//获取配置
string _directory = @ConfigurationSettings.AppSettings["down"];
string url = @ConfigurationSettings.AppSettings["url"];
if (urlFile == null)
{
Response.Write("<script>alert('似乎没有拿到文件名称?');</script>");
return;
}
string file = df(url,id,urlFile, _directory);
if (file.Contains(".xls"))
{
excel.ReadOnly = true;
excel.ShowFormulaBar = false;
excel.Open(_directory + file);
ASPxRichEdit1.Visible = false;
}
if (file.Contains(".doc"))
{
ASPxRichEdit1.Open(_directory + file);
ASPxRichEdit1.ReadOnly = true;
excel.Visible = false;
}
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="url">下载地址</param>
/// <returns>文件名称</returns>
[Obsolete]
public string df(string url,string id,string urlFile,string _directory)
{
try
{
string fileName = "";
if (urlFile.Contains(".xls")) fileName = "tmp.xlsx";
if (urlFile.Contains(".doc")) fileName = "tmp.docx";
if (!Directory.Exists(_directory))
{
Directory.CreateDirectory(_directory);
}
WebClient client = new WebClient();
url = url + "&id=" + id.ToString() + "&field=content&download=true&" + "filename=" + urlFile;
client.DownloadFile(url, _directory + fileName);
return fileName;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Response.Write("<script>alert('文件下载错误?');</script>");
return "";
}
}
}
}
...
<appSettings>
<!--文件下载目录-->
<add key="down" value="c:\tmp\"/>
<add key="url" value="http://127.0.0.1:666/web/cont?" />
</appSettings>
</configuration>