繁体   English   中英

如果不在本地主机上,则上传的文件以blob形式出现? 使用IIS Express的ASP.NET MVC4

[英]Uploaded file comes in as blob if not on localhost? asp.net mvc4 using IIS express

我的网站在我的本地网络上运行,允许用户上传zip文件。 但是,当我开始在局域网(而不是本地主机)上对其进行测试时,文件显示为blob吗? 例如:在运行IIS express的本地主机上,我可以上载example.zip,它显示在上载文件夹中,就像example.zip一样。 现在,如果我尝试从另一台计算机上载它,example.zip将显示为blob。 有趣的是,如果我将文件重命名为example.zip,并给出正确的扩展名,则文件是完整的,我可以读取它。 我以为可能是文件夹权限,所以我让所有人都可以对上传文件夹进行完全控制,以进行测试,但仍然无法正常工作。 这是我的API控制器中保存传入文件的代码。

public class UploadController : ApiController
{
    // Enable both Get and Post so that our jquery call can send data, and get a status
    [HttpGet]
    [HttpPost]
    public HttpResponseMessage Upload()
    {
        // Get a reference to the file that our jQuery sent.  Even with multiple files, they will all be their own request and be the 0 index
        HttpPostedFile file = HttpContext.Current.Request.Files[0];
        // do something with the file in this space
        if (File.Exists(HttpContext.Current.Server.MapPath("~/App_Data/uploads/test/" + file.FileName)))
        {
            Stream input = file.InputStream;
            FileStream output = new FileStream(HttpContext.Current.Server.MapPath("~/App_Data/uploads/test/" + file.FileName), FileMode.Append);
            byte[] buffer = new byte[8 * 1024];
            int len;
            while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, len);
            }
            input.Close();
            output.Close();
        }
        else
        {
            file.SaveAs(HttpContext.Current.Server.MapPath("~/App_Data/uploads/test/" + file.FileName));
        }
        // end of file doing


        // Now we need to wire up a response so that the calling script understands what happened
        HttpContext.Current.Response.ContentType = "text/plain";
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        var result = new { name = file.FileName};

        HttpContext.Current.Response.Write(serializer.Serialize(result));
        HttpContext.Current.Response.StatusCode = 200;

        // For compatibility with IE's "done" event we need to return a result as well as setting the context.response
        return new HttpResponseMessage(HttpStatusCode.OK);
    }

}

为什么我的文件另存为Blob有任何想法? 谢谢!

因此,又过了两个小时,试图弄清楚这一点,最终导致了Firefox中的一个错误。 我可以通过查看标题来获得真实的文件名。

var filenameHeader = HttpContext.Current.Request.Headers.Get("Content-Disposition");

然后,我使用Regex解析了文件名,因为它像这样:“ attachment; filename = \\” YourFileHere \\“”

这是指向我正确方向的链接: https : //groups.google.com/forum/#!topic/jquery-fileupload/RjfHLX2_EeM

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM