簡體   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