我有一个应用程序,需要从文件系统中读取PDF文件,然后将其写出给用户.PDF是183KB,似乎完美无缺.当我使用底部的代码时,浏览器获取一个224KB的文件,我从Acrobat Reader收到一条消息,说文件已损坏且无法修复.
这是我的代码(我也尝试过使用File.ReadAllBytes(),但我得到了相同的东西):
using (FileStream fs = File.OpenRead(path))
{
int length = (int)fs.Length;
byte[] buffer;
using (BinaryReader br = new BinaryReader(fs))
{
buffer = br.ReadBytes(length);
}
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", Path.GetFileName(path)));
Response.ContentType = "application/" + Path.GetExtension(path).Substring(1);
Response.BinaryWrite(buffer);
}
Run Code Online (Sandbox Code Playgroud) 我有一堆字符串我需要使用.Trim(),但它们可以为null.如果我可以做的事情会更简洁:
string endString = startString !?? startString.Trim();
Run Code Online (Sandbox Code Playgroud)
如果左侧的部分为非null,则基本上返回右侧的部分,否则只返回空值.我最后使用了三元运算符,但是为了这个目的,无论如何都要使用null-coalescing运算符?
我有一个Web服务,其方法有两个字符串参数.当我调试时,我可以在我的调用方法中看到它将两个字符串值传递给方法,但实际上WebMethod对于这两个值都是空的.这是一些代码:
的WebMethod
[WebMethod(Description = "Set username and password for validation purposes.")]
public void Login(string uname, string pword)
{
username = uname;
password = pword;
}
Run Code Online (Sandbox Code Playgroud)
呼叫方法
NewsletterEmailSubscribers nes = new NewsletterEmailSubscribers();
nes.Login("Username", "Password");
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
- 编辑 -
添加更多代码.
网络服务:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class NewsletterEmailSubscribers : WebService
{
private static string username, password;
public NewsletterEmailSubscribers()
{
}
/// <summary>
/// Logins the specified username.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
[WebMethod(Description = …
Run Code Online (Sandbox Code Playgroud) 我有一个Web应用程序,其中包含一个从数据库加载内容的页面.我希望能够在动态内容中放置一个表单,但.net不会让内部表单执行它的动作.有没有办法允许这种或其他方式我可以在动态内容页面上获取表单?
谢谢.
- 编辑 -
我想我需要澄清一些事情.这是一个从数据库加载内容的aspx页面.据我所知,我从数据库中提取并粘贴在标签中的文本永远不会被.net wp编译或处理,因此我不能使用后面的代码来解决这个问题.
我有一个带有以下ProcessRequest方法的IHttpHandler:
public void ProcessRequest(HttpContext context) {
int id = Convert.ToInt32(context.Request.QueryString["id"] + 151);
var xml = XDocument.Parse("<xml><cartid>" + id + "</cartid></xml>");
context.Response.Write(xml);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试从aspx页面使用如下:
protected void Page_Load(object sender, EventArgs e) {
order o = new order();
Server.Transfer(o, false);
}
Run Code Online (Sandbox Code Playgroud)
我得到一个HttpException:执行处理程序'PostTest.order'的子请求时出错.
如果我改为尝试做转移,如:
Server.Transfer("~/order.ashx?id=65", false)
Run Code Online (Sandbox Code Playgroud)
我得到一个HttpException:执行/order.ashx的子请求时出错.
我这样做错了还是有另一种方法来实现我想要的?
我有一个使用ClickOnce部署的Windows窗体应用程序,但是用户导航到网络驱动器并双击setup.msi而不是使用网页来安装它.
我有两个用户安装它,它在过去工作.我上周做了一个uppate,现在当用户尝试启动应用程序时,弹出"验证应用程序要求"的窗口,然后当它消失时,没有任何反应.询问是否可以更新的对话框永远不会弹出.
网络用户已经验证他们对网络驱动器具有权限(但是,如果这是问题,我会期望出现错误消息).
什么可能导致这种情况发生?
我正在使用 Visual Studio 2013,有一次我能够右键单击 web.config 转换文件,并且有一个“预览转换”选项,我可以单击该选项并查看文件转换的结果。我所拥有的任何解决方案都不再存在。
我如何才能恢复此选项,或者是进入转换预览的另一种方式?
我最近确实更新到了 Update 3。
程序吐出其中一个框,表示发生了未处理的异常,应用程序必须退出.解决问题的唯一线索就是在事件日志中:
事件类型:错误事件源:.NET运行时2.0错误报告事件类别:无事件ID:5000日期:1/9/2009时间:8:47:44 AM用户:N/A计算机:DADIEHL描述:EventType clr20r3,P1 crm.client.exe,P2 1.0.1.0,P3 49667f61,P4 mscorlib,P5 2.0.0.0,P6 471ebc5b,P7 c35,P8 59,P9 system.formatexception,P10 NIL.
所以我将以下代码添加到program.cs:
try
{
Application.Run(new WindowContainer());
}
catch (Exception exc)
{
new DialogException(exc).ShowDialog();
}
Run Code Online (Sandbox Code Playgroud)
只是这样我可以捕获任何异常,但用户仍然收到相同的消息,说应用程序必须退出.我无法在我的计算机上重现这一点,因此无法使用调试器来缩小范围.有谁知道如何收集更多信息或有任何想法是什么问题?
我继承了一个旧的应用程序,它将zip文件存储在数据库中,需要检索此文件.在Firefox中工作正常,我可以打开zip并且里面的每个文件都很好.当我在IE7中运行它时,我收到以下错误.
Internet Explorer无法从localhost下载ProductContentFormImage.aspx.
Internet Explorer无法打开此Internet站点.请求的网站不可用或无法找到.请稍后再试.
我正在使用下面的代码.
byte[] content = (byte[])Session["contentBinary"];
Response.ClearContent();
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;
Response.Expires = 0;
Response.ContentType = "application/zip";
Response.AddHeader("Content-Length", content.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=content.zip");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(content);
Response.End();
Run Code Online (Sandbox Code Playgroud) 我在edmx文件中添加了对存储过程的引用,然后右键单击它并选择"Create Function Import",它被添加到模型浏览器中EntityContainer下的Function Imports文件夹中.
据我了解,我应该可以像这样使用它:
sampleEntities db = new sampleEntities();
db.SampleStoredProcedure();
Run Code Online (Sandbox Code Playgroud)
但它没有显示在db对象上.我缺少一步吗?函数导入设置为public,没有返回值,以及我在展开它时可以看到的一个参数.
可能重复:
实体框架v2不支持sql 2000?
我有一个Windows窗体应用程序访问SQL Server 2000服务器.最初这个应用程序是用Visual Studio 2008编写的.从那时起我们已经迁移到Visual Studio 2010,当我尝试向.edmx添加新实体时(使用数据库中的更新模型),我收到一条消息,说EntityFramwork只适用于SQL Server 2005年或更高.
VS2010有什么变化吗?我之前绝对能够添加实体.
c# ×7
asp.net ×5
.net ×2
clickonce ×1
exception ×1
httphandler ×1
web-services ×1
winforms ×1
zip ×1