Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

c# - Uploaded file comes in as blob if not on localhost? asp.net mvc4 using IIS express

I have my site running on my local network which allows users to upload zip files. But when i started testing it on my local area network (not on the localhost) the files show up as blob? For example: On my localhost where IIS express is running i can upload example.zip and it shows up in the upload folder just fine as example.zip. Now if i try to upload it from another machine example.zip shows up as blob. Interestingly enough if i rename the file back to the example.zip giving the right extention the file is fully intact and i can read it. I thought it might be folder permissions so i gave the upload folder full control from everyone to test and it still doesn't work. Here is the code from my API controller that save the file coming in.

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);
    }

}

Any ideas why my files get saved as blob? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So after another couple hours trying to figure this out it ended up being a bug in Firefox that does this. I can get the real file name by looking at the headers.

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

I then used Regex to parse the filename out of it because it comes like this:"attachment; filename="YourFileHere""

Here is the link that pointed me in the right direction: https://groups.google.com/forum/#!topic/jquery-fileupload/RjfHLX2_EeM


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...