上传时不会破坏文件

时间:2012-05-10 06:44:32

标签: asp.net-mvc file-upload

我正在尝试将图像上传到我的IIS 7服务器,但它已经损坏了。相同的代码通过Visual Studio在本地实例上运行。

本地标题:

Request URL:http://localhost:55272/MyProfile/UploadPhoto?qqfile=01.jpg
Request Method:POST
Status Code:302 Found
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:464542
Content-Type:application/octet-stream
Host:localhost:55272
Origin:http://localhost:55272
Referer:http://localhost:55272/MyProfile
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1130.1 Safari/536.11
X-File-Name:01.jpg
X-Mime-Type:image/jpeg
X-Requested-With:XMLHttpRequest
Query String Parametersview URL encoded
qqfile:01.jpg
Response Headersview source
Cache-Control:private
Connection:Close
Content-Length:137
Content-Type:text/html; charset=utf-8
Date:Thu, 10 May 2012 06:35:28 GMT
Location:/MyProfile/MyProfile
Server:ASP.NET Development Server/10.0.0.0
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0

远程标头:

Request URL:[remotehost]/XMVC/MyProfile/UploadPhoto?qqfile=01.jpg
Request Method:POST
Status Code:302 Found
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Authorization:Negotiate -snip-
Connection:keep-alive
Content-Length:464542
Content-Type:application/octet-stream
Cookie:ASPSESSIONIDAABCCDSD=-snip-
Host:[remotehost]
Origin:[remotehost]
Referer:[remotehost]/XMVC
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1130.1 Safari/536.11
X-File-Name:01.jpg
X-Mime-Type:image/jpeg
X-Requested-With:XMLHttpRequest
Query String Parametersview URL encoded
qqfile:01.jpg
Response Headersview source
Cache-Control:private
Content-Length:142
Content-Type:text/html; charset=utf-8
Date:Thu, 10 May 2012 06:35:05 GMT
Location:/XMVC/MyProfile/MyProfile
Persistent-Auth:false
Server:Microsoft-IIS/7.5
WWW-Authenticate:Negotiate -snip-
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0
X-Powered-By:ASP.NET

以下是上传表单调用的上传方法:

[HttpPost]
public ActionResult UploadPhoto(object qqfile)
{
    // Get current logged in user
    var currentUser = _staffRepository.StaffMembers.First(p => p.Login == User.Identity.Name.Replace("Domain\\", ""));

    var length = Request.ContentLength;
    var bytes = new byte[length];
    Request.InputStream.Read(bytes, 0, length);
    // bytes has byte content here. what do do next?

    var fileName = currentUser.Login + ".jpg";

    var saveToFileLoc = string.Format("{0}\\{1}",
                                   Server.MapPath("~/App_Data/uploads"),
                                   fileName);

    // save the file.
    var fileStream = new FileStream(saveToFileLoc, FileMode.Create, FileAccess.ReadWrite);
    fileStream.Write(bytes, 0, length);
    fileStream.Close();

    return new RedirectResult("MyProfile");
}

如果相关,我使用此File Uploader

图像作为所有NUL字节在远程服务器上出现。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

原来这是一个Chrome错误,已经在稳定的19。10。884。5(5月23日发布)中得到修复。

具体修订:https://src.chromium.org/viewvc/chrome?view=rev&revision=138291

详细分析:http://inedo.com/support/kb/1019/workaround-for-chrome-file-uploading-bug