我有已转换的文件并将其插入到我的数据库中:
byte[] file = File.ReadAllBytes(outputpath);
string filesting = Convert.ToBase64String(file);
//INSERT INTO DB
Run Code Online (Sandbox Code Playgroud)
然后我从数据库中提取它并尝试下载它。
byte[] bytes = Convert.FromBase64String(file);
HttpContext.Current.Response.ContentType = "application/force-download";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=labtest.avi");
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.End();
Run Code Online (Sandbox Code Playgroud)
但没有下载任何内容。
为什么?
我正在尝试使用 symfony 从外部 URL 下载视频。所以我有这条路线,如果我直接在浏览器中点击它,我会收到两个可能的答案:视频已播放或(如果浏览器无法重现),视频是从这个外部 URL 下载的。我试图创建的是来自控制器的响应,以始终下载视频。我已经尝试了几种解决方案,但都没有运气。
一些尝试的解决方案:
$response = new BinaryFileResponse($url);
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
return $response;
Run Code Online (Sandbox Code Playgroud)
由于它不是静态文件,因此失败。
也试过
$response = new RedirectResponse($url);
$response->headers->set('Content-Type', 'video');
$d = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$videoUrl
);
$response->headers->set('Content-Disposition', $d);
Run Code Online (Sandbox Code Playgroud)
当然,“StreamedResponse”……我假设这是可能的,因为当浏览器中的格式没有正确转换时,我会得到所需的行为……感谢任何帮助。
我有一个 PHP 应用程序,它有一个路由器,它应该有两个默认处理程序401 Unauthorized和404 Not Found。
但也有可能没有为这两种情况设置处理程序。在这种情况下,我需要有一个默认响应,告诉Request Unhandled我已将其设置为响应代码400 Bad Request,但我觉得它不是真正合适的。
对于未处理的请求,我应该使用什么响应代码?
I'm new to Servlets I just trying to print a simple Html tag inside servlet response but I dunno why it doesn't print in Browser. it just print the String without getting the Html tags.
Here is the code:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Hi All");
PrintWriter out =response.getWriter();
out.println("<h2>Please complete our Customer Survey</h2>");
}
}
Run Code Online (Sandbox Code Playgroud)
**Out Put print as **
<h2>Please complete our Customer Survey</h2>
Run Code Online (Sandbox Code Playgroud)
Please let me know how to fix this Thanks.
我目前正在使用 .NET Core Web App 开发 API 以进行测试,但我一直坚持这个。
实际上,我有这个代码:
namespace CoreWebApp.API.Admin
{
[Route("api/country")]
public class CountryController : Controller
{
// GET: api/country
[HttpGet]
public HttpResponseMessage Get()
{
List<Country> countries = Shared.Database.SqlAction.CountriesTable.GetCountries();
return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(countries), Encoding.UTF8, "application/json") };
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是返回一个HttpStatusCode和一个HttpContent。然后我应该在 Postman 上得到它:
[
{
"Name":"France"
},
{
"Name":"Germany"
},
{
"Name":"Spain"
},
....
]
Run Code Online (Sandbox Code Playgroud)
状态代码 OK 200
但是,我根本没有得到这个身体,我得到的是:
{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": …Run Code Online (Sandbox Code Playgroud) 我正在尝试从我的服务器(使用 ASP.NET Core MVC)检索文件并将其显示在浏览器中。在浏览了此处和其他站点上的许多线程后,似乎每次都建议相同的解决方案;这是在响应标头中使用内联的 Content-Disposition。我的代码如下:
IFileInfo file = _fileProvider.GetFileInfo(filename);
Response.Headers.Add("Content-Disposition", $"inline; {file.Name}");
return File(file.CreateReadStream(), "application/pdf", file.Name);
Run Code Online (Sandbox Code Playgroud)
虽然这似乎是基于我见过的其他线程的正确代码,但 Content-Disposition 标头在我的代码中绝对没有任何作用。无论我有标题、删除它还是制作其他东西(即附件),它都会下载并保存文件,但除非我手动打开文件,否则不会显示它。
有谁知道我在这里想念什么?
httpresponse content-disposition response-headers asp.net-core-mvc .net-core
在我的 django 项目中,为了避免另一个请求,我想返回一个带有 HttpResponse 的 JsonResponse,如下所示:
JsonResponse({"known": True, "space": render(request, 'space.html')}, status=200)
Django进程返回正常的内部错误Object of type HttpResponse is not JSON serializable
我尝试了所有的网络解决方案(序列化器等),但找不到一种方法来返回带有字典条目的 json 格式(我的 javascript 必需的),其中一个是我可以与 then 一起使用的整个 html 页面$("body").html(response["space"])。
我错过了什么吗?
谢谢你的时间。
在那里我是烧瓶的新手。场景:我试图在提交表单后重定向到某个路由。所以我为此使用了烧瓶重定向以及代码参数。
@topics_bp.route('/create_topic/',methods =['GET','POST'] )
def create_topic():
if request.method == 'GET':
#send the form for create topic!
formData = TopicForm()
return render_template('create-topic.html',form = formData)
if request.method == 'POST':
# check the post method and redirect
return redirect(url_for('topic.topics'),code=201)
Run Code Online (Sandbox Code Playgroud)
基本上,我想为创建的记录返回 HTTP 代码 201,然后重定向到预期的路由。但是如果我这样做,它只会返回一个重定向页面。每次我都需要手动点击。

是否有任何解决方法可以返回 201 代码并自动重定向?提前致谢!
在 Chrome DevTools 的网络选项卡中,我右键单击请求并选择“Copy => Copy as PowerShell”并获得以下脚本。请求的“响应”是一个 json 字符串。
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
$session.Cookies.Add((New-Object System.Net.Cookie("INGRESSCOOKIE", "1661198715.885.38.892935", "/", "xx.com")))
$response = Invoke-WebRequest -UseBasicParsing -Uri "https://xx.com/api/v1/xxx?filter[reportDate]=2022-08-24&filter[forceReload]=true" `
-WebSession $session `
-Headers @{
"method"="GET"
"authority"="xx.com"
"scheme"="https"
"path"="/api/v1/sum? Filter[reportDate]=2022-08-24&filter[forceReload]=true"
"sec-ch-ua"="`"Chromium`";v=`"94`", `"Google Chrome`";v=`"94`", `";Not A Brand`";v=`"99`""
"accept"="application/vnd.api+json"
"authorization"="Bearer $($token. Token)"
"sec-ch-ua-mobile"="?0"
"sec-ch-ua-platform"="`"Windows`""
"origin"="https://xx.com"
"sec-fetch-site"="same-site"
"sec-fetch-mode"="cors"
"sec-fetch-dest"="empty"
"referrer"="https://xx.com/"
"accept-encoding"="gzip, deflate, br"
"accept-language"="en-US,en;q=0.9"
} `
-ContentType "application/vnd.api+json"
Run Code Online (Sandbox Code Playgroud)
但是,Write-Output $response.Content得到的是整数列表而不是 json …
我在 Google App Engine (GAE)Standard ENV 上托管的 Laravel 应用程序面临着一个令人困惑的问题。虽然应用程序在本地环境中完美运行,但在 GAE 上托管时我遇到了一个特定问题:大于约 450KB 的 API 响应永远不会完成;请求挂起,我没有收到任何错误,也没有触发任何日志。
任何有关类似问题的建议或经验都会非常有帮助。
httpresponse ×10
http ×3
php ×3
c# ×2
python ×2
.net-core ×1
asp.net ×1
asp.net-core ×1
django ×1
flask ×1
html ×1
httpcontent ×1
httprequest ×1
java ×1
jsonresponse ×1
laravel ×1
powershell ×1
response ×1
servlets ×1
symfony ×1