0

こんにちは、次のサブルーチンがあります。ファイルとして保存または保存するようにユーザーに依頼します。問題は、拡張子なしで保存することです。

ファイルの種類は GIF または PDF です。ルーチンが PDF か GIF かを自動的に判断する方法はありますか?

変数 sFullPath の拡張子は、たとえば sFullPath = C:\PICTURES\10603-1.gif または sFullPath = C:\PICTURES\10603-1.pdf です。

スクリーンショット、ファイルとして保存ダイアログが拡張子を与えていないことに注意してください


ここに画像の説明を入力


ここに画像の説明を入力


ここに画像の説明を入力


これが私のコードです

Private Sub DownloadFile(sFullPath As String, context As HttpContext)

    Dim size, start, [end], length As Long
    Dim fp As Long = 0
    Using reader As New StreamReader(sFullPath)

        size = reader.BaseStream.Length
        start = 0
        [end] = size - 1
        length = size

        ' Now that we've gotten so far without errors we send the accept range header 
        '/* At the moment we only support single ranges. 
        ' * Multiple ranges requires some more work to ensure it works correctly 
        ' * and comply with the spesifications: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2 
        ' * 
        ' * Multirange support annouces itself with: 
        ' * header('Accept-Ranges: bytes'); 
        ' * 
        ' * Multirange content must be sent with multipart/byteranges mediatype, 
        ' * (mediatype = mimetype) 
        ' * as well as a boundry header to indicate the various chunks of data. 
        ' */ 

        context.Response.AddHeader("Accept-Ranges", "0-" & size)

        ' header('Accept-Ranges: bytes'); 
        ' multipart/byteranges 
        ' http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2 

        If Not String.IsNullOrEmpty(context.Request.ServerVariables("HTTP_RANGE")) Then

            Dim anotherStart As Long = start
            Dim anotherEnd As Long = [end]

            Dim arr_split As String() = context.Request.ServerVariables("HTTP_RANGE").Split(New Char() {Convert.ToChar("=")})

            Dim range As String = arr_split(1)

            ' Make sure the client hasn't sent us a multibyte range 
            If range.IndexOf(",") > -1 Then

                '// (?) Shoud this be issued here, or should the first 
                '// range be used? Or should the header be ignored and 
                '// we output the whole content? 

                context.Response.AddHeader("Content-Range", "bytes " & start & "-" & [end] & "/" & size)

                Throw New HttpException(416, "Requested Range Not Satisfiable")
            End If

            '// If the range starts with an '-' we start from the beginning 
            '// If not, we forward the file pointer 
            '// And make sure to get the end byte if spesified 

            If (range.StartsWith("-")) Then

                '// The n-number of the last bytes is requested 

                anotherStart = size - Convert.ToInt64(range.Substring(1))



            Else

                arr_split = range.Split(New Char() {Convert.ToChar("-")})

                anotherStart = Convert.ToInt64(arr_split(0))
                Dim temp As Long = 0

                anotherEnd = If(arr_split.Length > 1 And Int64.TryParse(arr_split(1).ToString(), temp), Convert.ToInt64(arr_split(1)), size)

            End If

            '/* Check the range and make sure it's treated according to the specs. 
            ' * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html 
            ' */ 
            '// End bytes can not be larger than $end. 

            'anotherEnd = (anotherEnd > [end]) ? end : anotherEnd 
            anotherEnd = If((anotherEnd > [end]), [end], anotherEnd)
            '// Validate the requested range and return an error if it's not correct. 
            If (anotherStart > anotherEnd Or anotherStart > size - 1 Or anotherEnd >= size) Then
                context.Response.AddHeader("Content-Range", "bytes " & start & "-" & [end] & "/" & size)
                Throw New HttpException(416, "Requested Range Not Satisfiable")
            End If
            start = anotherStart
            [end] = anotherEnd

            length = [end] - start + 1 ' Calculate new content length 
            fp = reader.BaseStream.Seek(start, SeekOrigin.Begin)
            context.Response.StatusCode = 206
        End If
    End Using
    '// Notify the client the byte range we'll be outputting 
    context.Response.AddHeader("Content-Range", "bytes " & start & "-" & [end] & "/" & size)
    context.Response.AddHeader("Content-Length", length.ToString())
    '// Start buffered download 
    context.Response.WriteFile(sFullPath, fp, length)
    context.Response.End()
End Sub
4

2 に答える 2

1

content-disposition をヘッダーに追加して設定してみてください。これにより、ファイル名を指定できます。

于 2013-10-17T00:14:07.670 に答える
0

ダイアログ ボックスを開いてファイルを保存する場合、拡張子は空 ( . ) です。ファイル名を「YourFileName.txt」または「YourFileName.pdf」として保存します。それでも問題が解決しない場合は、拡張子を付けて別の名前を試してください。

于 2015-09-19T13:11:12.000 に答える