gpt4 book ai didi

javascript - Valums Uploader 额外请求负载添加到文件

转载 作者:行者123 更新时间:2023-11-30 06:34:25 25 4
gpt4 key购买 nike

我正在尝试在 VB 中使用 ASP.NET 设置 Valums 文件 uploader ( https://github.com/valums/file-uploader )。该文件当前正在上传并存储在我想要的位置并且似乎可以正常工作,但是 Firefox 和 Chrome(我假设 Safari 也是)似乎都将一些额外的文本附加到正在上传的实际 CSV 文件中。

例如,这是我在通过 Firefox 上传的文件中看到的:

-----------------------------31203180683076
Content-Disposition: form-data; name="qqfile"; filename="test.csv"
Content-Type: application/vnd.ms-excel

/* ACTUAL CONTENTS OF FILE HERE */

-----------------------------31203180683076--

同样,Chrome:

------WebKitFormBoundarytB00bSQcafSOAnmq
Content-Disposition: form-data; name="qqfile"; filename="test.csv"
Content-Type: application/vnd.ms-excel

/* ACTUAL CONTENTS OF FILE HERE */

------WebKitFormBoundarytB00bSQcafSOAnmq--

如果我在 Chrome 中查看网络面板,我确实看到上面发布的内容在请求负载中发送,但是我想删除它,或者在服务器写入文件时完全忽略它。当我将 paramsInBody: false 添加到 uploader 时,我以为我找到了解决方案,它确实消除了一些困惑,但不是全部。

客户端相关代码如下:

function createUploader() {
var uploader = new qq.FineUploader({
element: document.getElementById('bootstrapped-fine-uploader'),
request: {
paramsInBody: false,
endpoint: 'Uploader.ashx'
},
validation: {
allowedExtensions: ['csv', 'txt']
},
text: {
uploadButton: '<div><i class="icon-upload-alt icon-white"></i> Browse for files...</div>'
},
template: '<div class="span12"><div class="qq-uploader">' +
'<pre class="qq-upload-drop-area span12"><span>{dragZoneText}</span></pre>' +
'<div class="qq-upload-button btn btn-success" style="width: auto;">{uploadButtonText}</div>' +
'<span class="qq-drop-processing"><span class="qq-drop-processing-spinner"></span></span>' +
'<ul class="qq-upload-list" style="margin: 0; list-style-type: none; margin-top: 20px;"></ul>' +
'</div></div>',
classes: {
success: 'alert alert-success',
fail: 'alert alert-error'
}
});
}

这是相关的服务器端处理程序:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
On Error GoTo upload_error

Dim Request = context.Request
Dim strm As Stream = Request.InputStream
Dim br As BinaryReader = New BinaryReader(strm)
Dim fileContents() As Byte = {}
Const ChunkSize As Integer = 1024 * 1024

' We need to hand IE a little bit differently...
If Request.Browser.Browser = "IE" Then
Dim myfiles As System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files
Dim postedFile As System.Web.HttpPostedFile = myfiles(0)
If Not postedFile.FileName.Equals("") Then
Dim fn As String = System.IO.Path.GetFileName(postedFile.FileName)
br = New BinaryReader(postedFile.InputStream)
End If
End If

' Now have the binary reader on the IE file input Stream. Back to normal...
Do While br.BaseStream.Position < br.BaseStream.Length - 1
Dim b(ChunkSize - 1) As Byte
Dim ReadLen As Integer = br.Read(b, 0, ChunkSize)
Dim dummy() As Byte = fileContents.Concat(b).ToArray()
fileContents = dummy
dummy = Nothing
Loop

' Or write it to the filesystem:
Dim writeStream As FileStream = New FileStream("C:\TEMP\" & System.Guid.NewGuid.ToString() & ".csv", FileMode.Create)
Dim bw As New BinaryWriter(writeStream)
bw.Write(fileContents)
bw.Close()

' it all worked ok so send back SUCCESS is true!
context.Response.Write("{""success"":true}")
Exit Sub

upload_error:
context.Response.Write("{""error"":""An Error Occured""}")
End Sub

关于为什么它会以这种方式工作的任何想法?非常感谢。

最佳答案

我能够通过删除 VB 代码中的浏览器检查来解决这个问题。我没有检查 IE,而是让它在所有浏览器上运行。

在运行调试时,我注意到有些东西被拉出来了,我想使用该条件中的代码将其拉出来,所以我尝试使用 Chrome 运行它,结果运行良好。

VB 的代码现在看起来像这样。我所要做的就是注释掉条件并保持内部完好无损:

'Removed the If conditional and it worked.
'If Request.Browser.Browser = "IE" Then
Dim myfiles As System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files
Dim postedFile As System.Web.HttpPostedFile = myfiles(0)
If Not postedFile.FileName.Equals("") Then
Dim fn As String = System.IO.Path.GetFileName(postedFile.FileName)
br = New BinaryReader(postedFile.InputStream)
End If
'End If

关于javascript - Valums Uploader 额外请求负载添加到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15506467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com