在这个方法里加个断点,看下是在哪里报错的。
猜测是在 @"d:/" + nm 这里报错的
foreach (string f in Request.Files),到这就直接return了
@爛轲: 是不是 Request.Files 这里不对 客户端传过来的文件有问题
@nil: 嗯,好了搞好了,顺道问一下上传文件大于4M在哪里配置,客户端还是服务端?
@爛轲: IIS大文件上传需要改machine config 可以看下这篇文章 http://www.cnblogs.com/LifelongLearning/archive/2011/12/06/2278247.html
如果你是要上传文件的话,可以参考以下代码
public ActionResult FileUpLoad()
{
HttpPostedFileBase file = Request.Files["FileUp"];
if (file!=null)
{
string fileName = Path.GetFileName(file.FileName);
string fileExt = Path.GetExtension(fileName);
if (fileExt==".jpg")
{
var list = db.ImageServerInfo.Where(c => c.FlgUsable == true).ToList();
int count = list.Count();
Random r = new Random();
int rNum = r.Next();
int i = rNum % count;
ImageServerInfo model = list[i];
WebClient client = new WebClient();
string address = "http://" + model.ServerUrl + "/FileUp.ashx?serverId=" + model.ServerId + "&ext=" + fileExt;
client.UploadData(address, StreamToByte(file.InputStream));
return Content("文件上传成功");
}
else
{
return Content("文件类型错误");
}
}
else
{
return Content("文件不能为空");
}
}
private byte[] StreamToByte(Stream stream)
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Seek(0, SeekOrigin.Begin);
return buffer;
}
谢谢,不过不需要
foreach (string f in Request.Files)这句代码改成 foreach (string str in Request.Files.AllKeys)这个试下