使用jQuery上传文件插件时出错,附加到文件的不需要的字符串(WebKitFormBoundary)
我正在使用这个插件: http : //hayageek.com/docs/jquery-upload-file.php
我用它将文件发送到WCF Rest服务并将其保存在硬盘上。
上传工作正常,但问题是图像,exe等上传损坏。 如果我用文本编辑器打开上传的文件,我可以看到不需要的字符串
开始时:
—— WebKitFormBoundaryPUTfurckDMbpBxiw Content-Disposition:form-data; NAME = “文件”; filename =“image.png”内容类型:image / png
最后:
—— WebKitFormBoundaryPUTfurckDMbpBxiw–
我的服务代码:
Function GetFile(str As Stream, fileName As String, accion As String) As String Try Dim absFileName As String = "C:\inetpub\wwwroot\UploadedComponents\" & fileName Using fs As New FileStream(absFileName, FileMode.Create) str.CopyTo(fs) str.Close() End Using Return "Upload OK" Catch ex As Exception Throw ex End Try End Function
有什么想法解决这个问题?
最后我找到了答案:
从multipart / form-data POST读取文件输入
我需要从这里导入一个组件Multipart Parser 。
然后保存上传到服务上的文件,这样:
public void Upload(Stream stream) { string filepath = "some path with filename and extension"; // corrected filepath mistyping MultipartParser parser = new MultipartParser(stream); if (parser.Success) { // Save the file File.WriteAllBytes(filepath, parser.FileContents) } }