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 .
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;
}
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);
}
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;
}
It won't work due to httppostedfilebase only accept the value from view itself, or else it won get anything from the physical file. :)
You are still welcome to answer this question. Cause i found this is the limitation of httppostedfilebase.