Previous topicNext topic
Help > 开发指南 > API对接 > HttpServer > 事件 >
返回文件

我们还可以利用DownloadFile事件来解决一些下载小文件的场景。

Vb.Net
Dim e As SmHttpRequestEventArgs=Args(0)
'定义一个网站共享目录地址
Dim strPath As String="D:\wwwroot"
Dim ext As String  = Path.GetExtension(e.Path).ToLower()
If ext.Length<=0 Then '如果没有获得相应的文件名后缀,则认为不是要获取文件的,就直接跳过此程序
    Return True
End If
Dim strFullPath As String=Path.Combine(strPath,e.Path.Replace("/","\").TrimStart("\"))
If File.Exists(strFullPath) Then
    Select Case ext
        Case ".jpg",".gif",".png",".bmp",".js",".css" ,".html",".htm",".zip",".rar"
            '仅支持文件大小小于等于4M的文件
            e.SendSmallFile(strFullPath)
            Return False'这里返回False,告诉后面的程序不需要处理了。
    End Select
End If
'这里如果是其他场景,就继续执行后续的程序
Return True

C#
SmHttpRequestEventArgs e = (SmHttpRequestEventArgs)Args[0];
// 定义一个网站共享目录地址
string strPath = @"D:\wwwroot";
string ext = Path.GetExtension(e.Path).ToLower();
if (ext.Length <= 0) // 如果没有获得相应的文件名后缀,则认为不是要获取文件的,就直接跳过此程序
{
    return true;
}
string strFullPath = Path.Combine(strPath, e.Path.Replace("/","\\").TrimStart('\\'));
if (File.Exists(strFullPath))
{
    switch (ext)
    {
        case ".jpg":
        case ".gif":
        case ".png":
        case ".bmp":
        case ".js":
        case ".css":
        case ".html":
        case ".htm":
        case ".zip":
        case ".rar":
            // 仅支持文件大小小于等于4M的文件
            e.SendSmallFile(strFullPath);
            return false; // 这里返回False,告诉后面的程序不需要处理了。
    }
}
// 这里如果是其他场景,就继续执行后续的程序
return true;