我有一个全局拦截器,需要初始化我自己的请求上下文 DTO,并且我希望可以在处理当前请求的控制器中访问此 DTO。
到目前为止我找到的解决方案是创建 Request 范围内的可注入 RequestContext 类:
import {
Injectable,
Scope
} from '@nestjs/common';
import { Request } from 'express';
import { IncomingHttpHeaders } from 'http';
@Injectable({ scope: Scope.REQUEST })
export class RequestContext {
public headers: IncomingHttpHeaders;
....
initialize(request: Request) {
this.headers = request.headers;
.....
}
}
Run Code Online (Sandbox Code Playgroud)
并将此类注入拦截器:
import {
NestInterceptor,
ExecutionContext,
CallHandler,
Injectable,
Inject
} from '@nestjs/common';
import { Request } from 'express';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { RequestContext …Run Code Online (Sandbox Code Playgroud) 我想通过将一首歌作为另一首歌的背景混合成单个源来混合两个音频源.
例如,我有输入:
<input id="files" type="file" name="files[]" multiple onchange="handleFilesSelect(event)"/>
Run Code Online (Sandbox Code Playgroud)
和脚本解码这个文件:
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new window.AudioContext();
var sources = [];
var files = [];
var mixed = {};
function handleFilesSelect(event){
if(event.target.files.length <= 1)
return false;
files = event.target.files;
readFiles(mixAudioSources);
}
function readFiles(index, callback){
var freader = new FileReader();
var i = index ? index : 0;
freader.onload = function (e) {
context.decodeAudioData(e.target.result, function (buf) {
sources[i] = context.createBufferSource();
sources[i].connect(context.destination);
sources[i].buffer = buf;
if(files.length > i+1){
readFiles(i + …Run Code Online (Sandbox Code Playgroud) 在我的网站上,我可以选择下载用户上传的所有图像.问题出在带有希伯来名字的图像中(我需要文件的原始名称).我试图解码文件名,但这没有帮助.这是一个代码:
using ICSharpCode.SharpZipLib.Zip;
Encoding iso = Encoding.GetEncoding("ISO-8859-1");
Encoding utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(file.Name);
byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
string name = iso.GetString(isoBytes);
var entry = new ZipEntry(name + ".jpg");
zipStream.PutNextEntry(entry);
using (var reader = new System.IO.FileStream(file.Name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
byte[] buffer = new byte[ChunkSize];
int bytesRead;
while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
{
byte[] actual = new byte[bytesRead];
Buffer.BlockCopy(buffer, 0, actual, 0, bytesRead);
zipStream.Write(actual, 0, actual.Length);
}
}
Run Code Online (Sandbox Code Playgroud)
在utf-8编码之后,我得到了这样的希伯来文件名:??????.jpg我的错误在哪里?
我有一个 NestJS 应用程序,它将CoreModule核心服务的集合导出到所有其他模块。在核心模块中,我有动态模块寄存器HttpModule。我不会两次注册该模块以进行导入和导出,因此我执行以下操作:
const httpModule = HttpModule.register({
timeout: Configuration.requestTimeoutMilliseconds
});
@Module({
imports: [httpModule],
providers: [
...
],
exports: [
httpModule,
...
]
})
export class CoreModule { }
Run Code Online (Sandbox Code Playgroud)
当我启动我的应用程序时,我在InstanceLoader日志中看到HttpModule依赖项注册了两次:
一般导出动态模块的正确方法是什么?
我正在使用StackExchange.Redis并构建一些redis客户端界面RedisClientManager.在我的界面中,我有2个密钥设置器(按时间跨度和日期时间到期):
按时间跨度:
public void Set(string key, object value, TimeSpan timeout)
{
_cache.StringSet(key, Serialize(value), timeout);
}
Run Code Online (Sandbox Code Playgroud)
截止日期:
public void Set(string key, object value, DateTime expires)
{
_cache.StringSet(key, Serialize(value));
_cache.KeyExpire(key, expires);
}
Run Code Online (Sandbox Code Playgroud)
用法:
按时间跨度:
RedisClientManager.Set(o.Key, o, new TimeSpan(0, 0, 5, 0));
Run Code Online (Sandbox Code Playgroud)
截止日期:
RedisClientManager.Set(o.Key, o, DateTime.UtcNow.AddMinutes(5));
Run Code Online (Sandbox Code Playgroud)
如果我使用Timespan(第一种方法)添加新密钥,则该对象位于Redis缓存中,并在5分钟后过期.如果使用Date(第二种方法)添加新密钥,则该对象不会添加到Redis.此问题仅在服务器上发生.在localhost上一切正常.可能是redis使用本地服务器时间键吗?我该如何解决这个问题?
使用密钥设置绝对到期的正确方法是什么StackExchange.Redis?
我发现了很多与旧 V1 包相关的 gorm 模拟问题:github.com/jinzhu/gorm。使用github.com/DATA-DOG/go-sqlmock。
我在 v2 上没有找到太多东西。我的简单问题是:
假设我有这个存储包代码:
...
type Storage struct {
GormDB *gorm.DB
SqlDB *sql.DB
mutex sync.Mutex
ReadTimeout int
WriteTimeout int
}
func (ps *Storage) Open(settings *Settings) error {
if err := settings.Validate(); err != nil {
return err
}
ps.mutex.Lock()
defer ps.mutex.Unlock()
if ps.GormDB != nil {
return nil
}
gormDB, err := gorm.Open(postgres.New(postgres.Config{
DSN: settings.GetDSN(),
}), &gorm.Config{
SkipDefaultTransaction: true,
})
if err != nil {
return fmt.Errorf("%s: %v", DBConnectError, err)
} …Run Code Online (Sandbox Code Playgroud) 我需要两次读取csv文件.但在第一次阅读后:
using (var csvReader = new StreamReader(file.InputStream))
{
fileFullText += csvReader.ReadToEnd();
file.InputStream.Seek(0, SeekOrigin.Begin);
csvReader.Close();
}
Run Code Online (Sandbox Code Playgroud)
在另一个函数中使用文件:
public static List<string> ParceCsv(HttpPostedFileBase file)
{
//file.InputStream.Seek(0, SeekOrigin.Begin);
using (var csvReader = new StreamReader(file.InputStream))
{
// csvReader.DiscardBufferedData();
// csvReader.BaseStream.Seek(0, SeekOrigin.Begin);
string inputLine = "";
var values = new List<string>();
while ((inputLine = csvReader.ReadLine()) != null)
{
values.Add(inputLine.Trim().Replace(",", "").Replace(" ", ""));
}
csvReader.Close();
return values;
}
}
Run Code Online (Sandbox Code Playgroud)
file.Length是0.有人可以帮忙吗?
我在布局中的MVC3应用程序中获得了Getaway:
@if ((Request.Browser.Browser == "IE") && ((Request.Browser.MajorVersion == 7)))
{
//show some content
}
else
{
//show another content
}
Run Code Online (Sandbox Code Playgroud)
我有很多用户抱怨(用户使用Internet Explorer 8).他们从我的应用程序中看到Internet Explorer 7内容.我检测Internet Explorer 7版本的方式有什么问题?如何在我的应用程序中确保100%用户拥有Internet Explorer 7版本?可能这是特定的操作系统问题?
我的应用程序允许用户上传 jpeg/png 文件。我必须检测扩展名已更改的文件。例如:如果有人获取 example.txt 文件并将其名称更改为 example.jpg,那么通过 javascript 检查该文件是不够的,需要服务器解决方案。我怎样才能做到这一点?有人可以显示代码示例吗?谢谢
在我的网络应用程序中,我有链接"联系我们"这个链接是:
<a href="mailto:somemail@somemail.com" title="contactus">contact us</a>
Run Code Online (Sandbox Code Playgroud)
我的问题是:有没有办法在打开的电子邮件正文中加入一些内容?我的意思是,如果用户点击链接,他的默认电子邮件被打开,在这个陡峭的是有办法打开他的默认电子邮件与身体中的一些内容?谢谢
无法理解问题所在......我的溃败值是:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
我尝试传递参数ID:
@Html.ActionLink(app.Name, "SingleAppEdit", "Admin", new { id = app.Id }, null)
Run Code Online (Sandbox Code Playgroud)
我在Controller"Admin"中的操作:
public ActionResult SingleAppEdit(string appId)
{
var positions = new List<SelectListItem>
{
new SelectListItem() {Text = "Top", Value = "Top"},
new SelectListItem() {Text = "Bottom", Value = "Bottom"},
new SelectListItem() {Text = "None", Value = "None"}
};
ViewData["PositionsList"] = new SelectList(positions, "Value", …Run Code Online (Sandbox Code Playgroud) 我在客户端使用 Web Api (C#) 和 angular.js。我需要下载服务器响应内容(zip 的 ByteArrayContent)。我在服务器上有这个方法:
public HttpResponseMessage Download(DownloadImagesInput input)
{
if (!string.IsNullOrEmpty(input.ImageUrl))
{
byte[] imageBytes = GetByteArrayFromUrl(input.ImageUrl);
ZipManager manager = new ZipManager();
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
byte[] zipBytes;
zipBytes = string.IsNullOrEmpty(input.QrCode) ? manager.ZipFiles(imageBytes)
: manager.ZipFiles(imageBytes, input.QrCode);
result.Content = new ByteArrayContent(zipBytes);
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/zip");
return result;
}
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
Run Code Online (Sandbox Code Playgroud)
ZipManager 是我的服务,它只返回 zip 文件的字节数组。我需要在客户端下载这个 zip 存档。这是我的客户:
$apiService.downloadZip({ 'ImageUrl': $scope.currentImage, 'QrCode': str }).then(function (response) {
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:application/zip,' + response.data;
hiddenElement.target = …Run Code Online (Sandbox Code Playgroud) 我有三个脚本文件.main.js,script1.js,script2.js.
main.js只是将脚本包含在文档中:
function IncludeJs(sFileName) {
document.writeln('<script type="text/javascript" src="' + sFileName + '"></script>');
}
IncludeJs("../script1.js");
IncludeJs("../script2.js");
Run Code Online (Sandbox Code Playgroud)
script1.js和script2.js代码位于相同的命名空间下.
script1.js:
var sharedNamespace = sharedNamespace || {};
(function () {
"use strict";
function func1(){
....
}
}).apply(sharedNamespace );
Run Code Online (Sandbox Code Playgroud)
script2.js:
var sharedNamespace = sharedNamespace || {};
(function () {
"use strict";
function func2(){
return func1();
}
}).apply(sharedNamespace );
Run Code Online (Sandbox Code Playgroud)
func2无法正常工作,因为func1未定义.我如何将script1.js和script2.js放在同一范围内的共享变量?
解决方案sharedNamespace.func1 = function(){}对我来说是最糟糕的,因为我不想将此功能公开给使用我的库的客户端...
c# ×6
asp.net ×5
.net ×3
javascript ×3
nestjs ×2
typescript ×2
angularjs ×1
asp.net-mvc ×1
file ×1
go ×1
go-gorm ×1
html5-audio ×1
node.js ×1
unit-testing ×1
zip ×1