上传的文件如果不在本地主机上,会以blob的形式出现吗?asp.net mvc4使用IIS express
本文关键字:asp net express IIS 使用 mvc4 blob 如果不 文件 会以 主机 | 更新日期: 2023-09-27 18:29:17
我的网站在本地网络上运行,允许用户上传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/#!主题/jquery文件上传/RjfHLX2_EeM