我有许多页面需要支持将数据导出到Excel电子表格.我可以很好地生成Excel文件,但我正在尝试弄清楚如何抽象这种行为,以便它可以从我需要的所有页面轻松地重用.我目前的想法是使用静态实用方法,如下所示:
public static void SendExcelFile(System.Web.UI.Page callingPage, string downloadFileName, List<List<string>> data, string worksheetTitle)
{
string tempFileName = Path.GetTempFileName();
try
{
// Generate file using ExcelPackage
GenerateExcelDoc(tempFileName, data, worksheetTitle);
callingPage.Response.AddHeader("Content-Disposition", "attachment;filename=" + downloadFileName);
callingPage.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
callingPage.Response.AddHeader("Content-Length", new FileInfo(tempFileName).Length.ToString());
callingPage.Response.TransmitFile(tempFileName);
}
finally
{
//When this is removed, the method works as expected.
if (File.Exists(tempFileName))
File.Delete(tempFileName);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在调用的单击处理程序SendExcelFile如下所示:
protected void lnkExport_Click(object sender, EventArgs e)
{
List<List<string>> dataList = GatherDataForSpreadsheet();
Utility.SendExcelFile(this, "fileNameForDownload.xlsx", dataList, "MyReports");
}
Run Code Online (Sandbox Code Playgroud)
这段代码可以很好地作为调用页面的实例方法.但是,作为静态方法,它根本不起作用.当我单击调用此按钮时,浏览器会无限期地显示加载动画,但从不提示文件下载.
我对ASP.NET(以及一般的Web编程)很新,所以我确定我在这里遗漏了一些东西.有人可以解释一下我所看到的行为,并建议一种合理的替代方法吗?
编辑:如果我最后删除对File.Delete()的调用,该方法按预期工作.Response.TransmitFile()是否异步进行传输?
编辑2:我只需要在删除文件之前调用Response.Flush().请参阅下面的答案.谢谢!
我正在制作一些视图函数来计算社区中一个用户的排名.我的问题是,我想在每个用户的个人资料中显示传感器的级别,我不知道如何,因为我没有;请求和render_to_response(因为我猜不需要他们)我的代码:
def calculate_questions_vote(request):
useranswer = Answer.objects.filter (answer_by = request.user)
positive_votes = VoteUpAnswer.objects.filter(answer = useranswer)
negative_votes = VoteDownAnswer.objects.filter(answer = useranswer)
question_vote_rank = sum(positive_votes) - sum(negative_votes.count)
return question_vote_rank
def calculate_replies(request):
the_new = News.objects.filter(created_by = request.user)
reply = Reply.objects.filter(reply_to = the_new)
reply_rank = sum(reply)
return reply_rank
def calculate_votes(request):
the_new = News.objects.filter(created_by = request.user)
vote = Vote.objects.filter(voted = the_new)
vote_rank = sum(vote)
return vote_rank
def personal_rank(request):
personal_rank = question_vote_rank + reply_rank + vote_rank
return personal_rank
Run Code Online (Sandbox Code Playgroud)
在UserProfiles中:
user = request.user
personal_rank = calculate_questions_vote(user) + calculate_replies(user) …Run Code Online (Sandbox Code Playgroud) 从资源清理的角度看,为什么有Response.Close()和Response.Dispose(),哪一个更全面的(调用另一个)?
我打电话给第三方Web服务并使用以下格式的消息返回ESOAPHTTPException:
这里有神秘的消息 - URL:http:// webserviceurl - SOAPAction:performWithArgList
现在,此Web服务的支持人员已要求完整的SOAP响应.通常,我会将事件处理程序附加到THTTPRIO.OnAfterExecute并简单地存储我作为参数接收的流的内容.
但是由于Delphi引发了异常,因此该事件处理程序不会执行.我理解异常可能实际上意味着服务以某种灾难性的方式失败了,但仍然应该有某种响应(不是超时错误).
在 Delphi将其转换为异常之前,我是否可以使用其他方法来捕获响应?
我有一个Apache服务器,我想开始用Django编写的网站.我用户mod_wsgi.现在我做好了准备.但是服务器的响应是空的.而在错误日志中,什么也没有.你知道为什么吗?
如果某个文件可以提供帮助(*.wsgi,settings.py),我会附加它.
Prochazky.wsgi
import os
import sys
import site
os.environ['PYTHON_EGG_CACHE'] = '/home/prochazky/venv/.python-eggs'
site.addsitedir('/home/prochazky/venv/lib/python2.6/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = 'Prochazky.settings'
sys.path.append('/home/prochazky/')
sys.path.append('/home/prochazky/Prochazky/')
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Run Code Online (Sandbox Code Playgroud)
Apache vhost:
<VirtualHost *:80>
WSGIScriptAlias / /home/prochazky/Prochazky/Prochazky.wsgi
ServerName testing.prochazky.net
DocumentRoot /home/prochazky
ErrorLog /home/prochazky/wsgi.log
</VirtualHost>
Run Code Online (Sandbox Code Playgroud) 以下是WCF服务我希望同时从用户那里获得5个值,并向他们发送添加前两个值的响应作为AddedResult,添加结果的减法和第三个值作为SubtractedResult,SubtractedResult和第四个值的乘法作为MultipliedResult和multipliedResult和第5个值的分割为DividedResult.
我知道这看起来没有意义,但我正在尝试用这些进行一些预先测试,但我是WCF的新手,任何可以提供帮助的人都会非常感激.
public interface IService1
{
[OperationContract]
string GetValuesForCalculation(int value1, int value2, int value3, int value4, int value5);
// TODO: Add your service operations here
}
public class Service1 : IService1
{
public int GetValuesForCalculation(int value1, int value2, int value3, int value4, int value5)
{
int AddedResult;
int SubtractedResult;
int MultipliedResult;
int DividedResult;
AddedResult = (value1 + value2);
SubtractedResult = (AddedResult - value3);
MultipliedResult = (SubtractedResult - value4);
DividedResult =(MultipliedResult/value5);
return AddedResult;
return SubtractedResult;
return MultipliedResult;
return DividedResult;
}
} …Run Code Online (Sandbox Code Playgroud) 在HTTP中,我知道标准的200响应最后带有“ OK”。但是我似乎找不到其他代码(例如403)是否需要OK。
例如(来自维基百科):
HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Etag: "3f80f-1b6-3e1cb03b"
Content-Type: text/html; charset=UTF-8
Content-Length: 131
Connection: close
<html>
<head>
<title>An Example Page</title>
</head>
<body>
Hello World, this is a very simple HTML document.
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
因此,在403响应中它将以:
HTTP/1.1 403 OK
Run Code Online (Sandbox Code Playgroud)
要么:
HTTP/1.1 403 FORBIDDEN
Run Code Online (Sandbox Code Playgroud)
要不就:
HTTP/1.1 403
Run Code Online (Sandbox Code Playgroud) 我有一个URL,在调用时会重定向到另一个URL.我想传递一些数据和重定向.
例如,我有这个方法:
@RequestMapping("efetuaEnvioEmail")
public String efetuaEnvioEmail(HttpServletResponse response) throws IOException {
System.out.println("efetuaEnvioEmail");
return "redirect:inicio";
}
Run Code Online (Sandbox Code Playgroud)
哪个重定向到这个:
@RequestMapping("inicio")
public String Inicio(HttpServletRequest request) throws IOException {
return "Inicio";
}
Run Code Online (Sandbox Code Playgroud)
我想传递一些数据,告知第一种方法一切正常.
我已经尝试了HttpServletRequest和HttpServletResponse的一些方法,但我没有任何东西.
我有一些查询似乎返回了大量数据,但我想知道多少。
只需序列化数据并将其转储到文件中,是否可以通过编程方式选择查询响应的字节大小?
我只是在搞乱API,现在我正试图在我的应用程序中使用Google Directions API.
我创建了一个表单来获取用户的输入并检索此数据并在routes.php文件中创建URI :
Route::get('/directions', function() {
$origin = Input::get('origin');
$destination = Input::get('destination');
$url = "http://maps.googleapis.com/maps/api/directions/json?origin=" . $origin . "&destination=" . $destination . "&sensor=false";
});
Run Code Online (Sandbox Code Playgroud)
该URL的响应是JSON.但是现在,我怎么能与Laravel一起存储这个回复?我一直在看Laravel中的Http命名空间,但我找不到方法.如果用Laravel无法完成,怎么用普通的php完成?