我可以用其他名称保存查询字符串的下载链接吗?

问题描述 投票:0回答:1

假设我有100个联系:

video.mp4? 1234video.mp4? 1221....

当我想下载这些链接时,它归结为video.mp4。

我可以这样更改吗?

当我要下载它时,它应该自动以该名称保存:

1234-video.mp41221-video.mp4

是否可以使用任何编程语言来做到这一点?

如果我对特殊的浏览器进行编码,是否可以使用c#执行类似的操作?

c# string browser
1个回答
0
投票

是否可以使用任何编程语言来做到这一点?

我可以用C#做​​这样的事情吗?

[使用IIS 7.0+并为集成模式配置了应用程序池时,您可以处理所有请求(除动态文件(例如aspx,add,svc等)之外的静态文件请求。

您还可以在开发期间使用IISExpress(这是VS 2017用于Web项目的默认Web服务器)进行此操作

假设您有一个程序集名称为WebApplication1的Web应用程序>

在您的web.config文件中,将以下属性(runAllManagedModulesForAllRequests)设置为true,并添加新的HTTP模块定义,如下所示:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
        <add name="VideoDownloadHandler" type="WebApplication1.VideoDownloadHandler"/>
    </modules> 
</system.webServer>

然后将VideoDownloadHandler.cs实现为Web项目中的类,如下所示:(此代码假定您在/ vid文件夹下有视频文件video.mp4)

using System;
using System.Web;

namespace WebApplication1
{
    public class VideoDownloadHandler : IHttpModule
    {
        public void Dispose()
        {
            // Nothing to dispose
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += context_BeginRequest;
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpRequest request = (sender as HttpApplication).Request;
            HttpResponse response = (sender as HttpApplication).Response;
            HttpApplication application = sender as HttpApplication;

            if (!request.Url.AbsolutePath.ToLowerInvariant().EndsWith("video.mp4"))
            {
                return;
            }

            string queryString = request.QueryString.ToString();
            string videoFileName = string.IsNullOrEmpty(queryString) ? "video.mp4" : queryString + "-video.mp4";

            response.ContentType = "video/mp4";
            response.AddHeader("content-disposition", "attachment;filename=" + videoFileName + ";");
            response.TransmitFile("~/vid/video.mp4");
            response.Flush();
            application.CompleteRequest();
        }
    }
}

当您的应用程序收到对video.mp4的请求时,它将由HttpModule处理,但所有其他请求将留待进一步处理。

如您所见,您可以给下载的文件起任何名字(这里我们给它起的名字queryString + "-video.mp4"

© www.soinside.com 2019 - 2024. All rights reserved.