文件上传:无法将值分配给文件,对我持续弹出null吗?需要帮助吗? [英] File Upload: Fail to assign value into File, keep poping null to me? Need help, any pro?

查看:147
本文介绍了文件上传:无法将值分配给文件,对我持续弹出null吗?需要帮助吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用asp mvc上传文件,它在第一次尝试将文件附加到模型中时非常有效,文件是system.web.httpfilewrapper.

I'm doing an upload in asp mvc, it is working great at first user attemp to attach a file into model, file is system.web.httpfilewrapper .

但是在第二次尝试时,控制器从其他文本框中捕获到无效金额,并回发给用户说无效,此后用户输入有效金额,然后返回到控制器,此处开始让我不知道发生了什么,文件突然变成null,无法完成将值发布到模型中的以下代码行.

But when it comes to second attemp where controller catching an invalid amount from other textbox, and it postback to the user saying invalid, thereafter the user type in a valid amount, and it goes back to the controller, here here then started making me have no idea what happened, file got suddenly went into null and it cant finish the following line of code that post the value into the model.

if (model.File != null && model.File.ContentLength > 0)
{
    Byte[] destination = new Byte[model.File.ContentLength];
    model.File.InputStream.Position = 0;
    model.File.InputStream.Read(destination, 0, model.File.ContentLength);
    model.BankSlip = destination;
}

毕竟,我决定使用另一种方法来完成它,因为model.file是= null,所以我在第一块添加了一些代码,以使文件保存到App_Data中以临时的方式保存,并在以后检索上...

After all i decide to use another method to finish it, since model.file is =null, so i added some code at the first block to let file save into App_Data for temporary and retrieve later on...

if (model.File != null && model.File.ContentLength > 0)
{
    Byte[] destination2 = new Byte[model.File.ContentLength];
    model.File.InputStream.Position = 0;
    model.File.InputStream.Read(destination2, 0, model.File.ContentLength);
    model.BankSlip = destination2;
    string chg = model.File.ToString();
    string file = Path.GetFileName(chg);
    string NewName = chg.Replace(file, member.MemberCode+".jpg");
    var fileName = Path.GetFileName(NewName);
    var savepath = Path.Combine(Server.MapPath("~/App_Data"), fileName);
    model.File.SaveAs(savepath);

}

到目前为止,它一直运行良好,但是问题在else块中开始,我使用文件流将文件读回并尝试将其重新定位到模型中,一切都很好 除外,该文件仍为= Null.这是我问的愚蠢问题,我不知道如何将系统io分配或转换为httppostedfilebase,它不断向我显示一个被注释掉的错误,任何专业人士都可以帮助我这个愚蠢的想法?我知道要解决这个问题,我实际上可以使用会话,但是由于某些原因,我不想使用它,而是使用更长的方法来完成它,如果有人可以解决这个问题,我将不胜感激.

It working perfectly until this point, but problem started in the else block, im using filestream to read the file back and trying to locate it back to the model, all is working good Except the file is still =Null. Here is the stupid question im asking, i don't know how to assign or convert the system io into httppostedfilebase, it keep showing me error which the one commented out, any pro can help my silly idea? I know to solve this, i can actually using session, but for certain reason, i don't want to use it, instead i use the longer way to do it, i ll be appreciated if anyone can solve this out.

else
{
       FileStream fileStream = new FileStream(Path.Combine(Server.MapPath("~/App_Data"), member.MemberCode + ".jpg"), FileMode.Open, FileAccess.Read);
       int read = (int)fileStream.Length;
      // HttpPostedFileBase colbase = new HttpPostedFileWrapper();
       //model.File = colbase;
       Byte[] destination = new Byte[read];
       fileStream.Position = 0;
       fileStream.Read(destination, 0, read);
       model.BankSlip = destination;
}

推荐答案

由于httppostedfilebase仅接受视图本身的值,因此它将无法工作,否则它将无法从物理文件中获取任何信息. :)

It won't work due to httppostedfilebase only accept the value from view itself, or else it won get anything from the physical file. :)

仍然欢迎您回答此问题.因为我发现这是httppostedfilebase的限制.

You are still welcome to answer this question. Cause i found this is the limitation of httppostedfilebase.

这篇关于文件上传:无法将值分配给文件,对我持续弹出null吗?需要帮助吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆