How to parse byte[] array from model with IFormFile from viewmodel for file uploading controller?
I have Core MVC application, when for specific entity I have following model:
public class Aktualnosci
public long ID get; set;
public string Tytul get; set;
public string Tresc get; set;
public DateTime Dzien get; set;
public byte AktualnosciImage get; set;
For uploading the image I am using IFormFile property in my viewmodel, that is called by controller, according to the documentation from >>HERE<<:
public class AktualnosciCreateVM
public long ID get; set;
[Required(ErrorMessage = "Proszę wypełnić pole.")]
[StringLength(40, ErrorMessage = "Max 40 znaków.")]
public string Tytul get; set;
[Required(ErrorMessage = "Proszę wypełnić pole.")]
public string Tresc get; set;
[Required(ErrorMessage = "Proszę wypełnić pole.")]
public DateTime Dzien get; set;
public IFormFile AktualnosciImage set; get;
And it is used for creating and editing of the entity. Right now I have troubles with parsing public IFormFile AktualnosciImage set; get; and public byte AktualnosciImage get; set; in my controller's GET method, to have returned viewmodel:
[Authorize(Roles = "Moderatorzy")]
// GET: Aktualnosci/Edit/5
public IActionResult Edit(long? id)
if (id == null)
return NotFound();
Aktualnosci aktualnosci = aktualnosciRepository.AktualnosciList
.FirstOrDefault(m => m.ID == id);
if (aktualnosci == null)
return NotFound();
else
aktualnosciCreateVM.ID = aktualnosci.ID;
aktualnosciCreateVM.Tytul = aktualnosci.Tytul;
aktualnosciCreateVM.Tresc = aktualnosci.Tresc;
aktualnosciCreateVM.Dzien = aktualnosci.Dzien;
//this one gives me an error v
aktualnosciCreateVM.AktualnosciImage = aktualnosci.AktualnosciImage.ToArray();
return View(aktualnosciCreateVM);
The compilation error is:
Cannot implicitly convert type 'byte' to
'Microsoft.AspNetCore.Http.IFormFile'
Is there any way to parse this 2 properties?
c# asp.net-core-mvc entity-framework-core iformfile
|
show 1 more comment
I have Core MVC application, when for specific entity I have following model:
public class Aktualnosci
public long ID get; set;
public string Tytul get; set;
public string Tresc get; set;
public DateTime Dzien get; set;
public byte AktualnosciImage get; set;
For uploading the image I am using IFormFile property in my viewmodel, that is called by controller, according to the documentation from >>HERE<<:
public class AktualnosciCreateVM
public long ID get; set;
[Required(ErrorMessage = "Proszę wypełnić pole.")]
[StringLength(40, ErrorMessage = "Max 40 znaków.")]
public string Tytul get; set;
[Required(ErrorMessage = "Proszę wypełnić pole.")]
public string Tresc get; set;
[Required(ErrorMessage = "Proszę wypełnić pole.")]
public DateTime Dzien get; set;
public IFormFile AktualnosciImage set; get;
And it is used for creating and editing of the entity. Right now I have troubles with parsing public IFormFile AktualnosciImage set; get; and public byte AktualnosciImage get; set; in my controller's GET method, to have returned viewmodel:
[Authorize(Roles = "Moderatorzy")]
// GET: Aktualnosci/Edit/5
public IActionResult Edit(long? id)
if (id == null)
return NotFound();
Aktualnosci aktualnosci = aktualnosciRepository.AktualnosciList
.FirstOrDefault(m => m.ID == id);
if (aktualnosci == null)
return NotFound();
else
aktualnosciCreateVM.ID = aktualnosci.ID;
aktualnosciCreateVM.Tytul = aktualnosci.Tytul;
aktualnosciCreateVM.Tresc = aktualnosci.Tresc;
aktualnosciCreateVM.Dzien = aktualnosci.Dzien;
//this one gives me an error v
aktualnosciCreateVM.AktualnosciImage = aktualnosci.AktualnosciImage.ToArray();
return View(aktualnosciCreateVM);
The compilation error is:
Cannot implicitly convert type 'byte' to
'Microsoft.AspNetCore.Http.IFormFile'
Is there any way to parse this 2 properties?
c# asp.net-core-mvc entity-framework-core iformfile
You cannot set the value of a file input programatically (it can only be set by the user selecting a file in the browser), so it would be pointless to try and set the value of theIFormFileproperty
– user3559349
Nov 14 '18 at 10:30
@StephenMuecke In that case how can I pass img from database to my VM, if in VM I haveIFormFiletype property, that is required to upload the file?
– Przemyslaw.Pszemek
Nov 14 '18 at 10:54
2
IFormFileis for uploading a file only. Its not clear what you want. If you want to display an image in the view (to indicate what the user previously uploaded), then you can convert thebyteto aBase64Stringand display it in an<img>element. Although typically you would save the uploaded file to the server and just save the path/display name in the db, and the include that in the view
– user3559349
Nov 14 '18 at 11:02
@StephenMuecke good point. I removed alreadyaktualnosciCreateVM.AktualnosciImage = aktualnosci.AktualnosciImage.ToArray();from my controller. Just right now I am gettingNullReferenceException: Object reference not set to an instance of an object.forawait aktualnosci.AktualnosciImage.CopyToAsync(memoryStream);. I need to figure out what I did wrong...
– Przemyslaw.Pszemek
Nov 14 '18 at 11:05
I cannot see anything in your question aboutawait aktualnosci.AktualnosciImage.CopyToAsync(memoryStream)- you will need to ask a new question about that
– user3559349
Nov 14 '18 at 11:08
|
show 1 more comment
I have Core MVC application, when for specific entity I have following model:
public class Aktualnosci
public long ID get; set;
public string Tytul get; set;
public string Tresc get; set;
public DateTime Dzien get; set;
public byte AktualnosciImage get; set;
For uploading the image I am using IFormFile property in my viewmodel, that is called by controller, according to the documentation from >>HERE<<:
public class AktualnosciCreateVM
public long ID get; set;
[Required(ErrorMessage = "Proszę wypełnić pole.")]
[StringLength(40, ErrorMessage = "Max 40 znaków.")]
public string Tytul get; set;
[Required(ErrorMessage = "Proszę wypełnić pole.")]
public string Tresc get; set;
[Required(ErrorMessage = "Proszę wypełnić pole.")]
public DateTime Dzien get; set;
public IFormFile AktualnosciImage set; get;
And it is used for creating and editing of the entity. Right now I have troubles with parsing public IFormFile AktualnosciImage set; get; and public byte AktualnosciImage get; set; in my controller's GET method, to have returned viewmodel:
[Authorize(Roles = "Moderatorzy")]
// GET: Aktualnosci/Edit/5
public IActionResult Edit(long? id)
if (id == null)
return NotFound();
Aktualnosci aktualnosci = aktualnosciRepository.AktualnosciList
.FirstOrDefault(m => m.ID == id);
if (aktualnosci == null)
return NotFound();
else
aktualnosciCreateVM.ID = aktualnosci.ID;
aktualnosciCreateVM.Tytul = aktualnosci.Tytul;
aktualnosciCreateVM.Tresc = aktualnosci.Tresc;
aktualnosciCreateVM.Dzien = aktualnosci.Dzien;
//this one gives me an error v
aktualnosciCreateVM.AktualnosciImage = aktualnosci.AktualnosciImage.ToArray();
return View(aktualnosciCreateVM);
The compilation error is:
Cannot implicitly convert type 'byte' to
'Microsoft.AspNetCore.Http.IFormFile'
Is there any way to parse this 2 properties?
c# asp.net-core-mvc entity-framework-core iformfile
I have Core MVC application, when for specific entity I have following model:
public class Aktualnosci
public long ID get; set;
public string Tytul get; set;
public string Tresc get; set;
public DateTime Dzien get; set;
public byte AktualnosciImage get; set;
For uploading the image I am using IFormFile property in my viewmodel, that is called by controller, according to the documentation from >>HERE<<:
public class AktualnosciCreateVM
public long ID get; set;
[Required(ErrorMessage = "Proszę wypełnić pole.")]
[StringLength(40, ErrorMessage = "Max 40 znaków.")]
public string Tytul get; set;
[Required(ErrorMessage = "Proszę wypełnić pole.")]
public string Tresc get; set;
[Required(ErrorMessage = "Proszę wypełnić pole.")]
public DateTime Dzien get; set;
public IFormFile AktualnosciImage set; get;
And it is used for creating and editing of the entity. Right now I have troubles with parsing public IFormFile AktualnosciImage set; get; and public byte AktualnosciImage get; set; in my controller's GET method, to have returned viewmodel:
[Authorize(Roles = "Moderatorzy")]
// GET: Aktualnosci/Edit/5
public IActionResult Edit(long? id)
if (id == null)
return NotFound();
Aktualnosci aktualnosci = aktualnosciRepository.AktualnosciList
.FirstOrDefault(m => m.ID == id);
if (aktualnosci == null)
return NotFound();
else
aktualnosciCreateVM.ID = aktualnosci.ID;
aktualnosciCreateVM.Tytul = aktualnosci.Tytul;
aktualnosciCreateVM.Tresc = aktualnosci.Tresc;
aktualnosciCreateVM.Dzien = aktualnosci.Dzien;
//this one gives me an error v
aktualnosciCreateVM.AktualnosciImage = aktualnosci.AktualnosciImage.ToArray();
return View(aktualnosciCreateVM);
The compilation error is:
Cannot implicitly convert type 'byte' to
'Microsoft.AspNetCore.Http.IFormFile'
Is there any way to parse this 2 properties?
c# asp.net-core-mvc entity-framework-core iformfile
c# asp.net-core-mvc entity-framework-core iformfile
asked Nov 14 '18 at 10:18
Przemyslaw.PszemekPrzemyslaw.Pszemek
112111
112111
You cannot set the value of a file input programatically (it can only be set by the user selecting a file in the browser), so it would be pointless to try and set the value of theIFormFileproperty
– user3559349
Nov 14 '18 at 10:30
@StephenMuecke In that case how can I pass img from database to my VM, if in VM I haveIFormFiletype property, that is required to upload the file?
– Przemyslaw.Pszemek
Nov 14 '18 at 10:54
2
IFormFileis for uploading a file only. Its not clear what you want. If you want to display an image in the view (to indicate what the user previously uploaded), then you can convert thebyteto aBase64Stringand display it in an<img>element. Although typically you would save the uploaded file to the server and just save the path/display name in the db, and the include that in the view
– user3559349
Nov 14 '18 at 11:02
@StephenMuecke good point. I removed alreadyaktualnosciCreateVM.AktualnosciImage = aktualnosci.AktualnosciImage.ToArray();from my controller. Just right now I am gettingNullReferenceException: Object reference not set to an instance of an object.forawait aktualnosci.AktualnosciImage.CopyToAsync(memoryStream);. I need to figure out what I did wrong...
– Przemyslaw.Pszemek
Nov 14 '18 at 11:05
I cannot see anything in your question aboutawait aktualnosci.AktualnosciImage.CopyToAsync(memoryStream)- you will need to ask a new question about that
– user3559349
Nov 14 '18 at 11:08
|
show 1 more comment
You cannot set the value of a file input programatically (it can only be set by the user selecting a file in the browser), so it would be pointless to try and set the value of theIFormFileproperty
– user3559349
Nov 14 '18 at 10:30
@StephenMuecke In that case how can I pass img from database to my VM, if in VM I haveIFormFiletype property, that is required to upload the file?
– Przemyslaw.Pszemek
Nov 14 '18 at 10:54
2
IFormFileis for uploading a file only. Its not clear what you want. If you want to display an image in the view (to indicate what the user previously uploaded), then you can convert thebyteto aBase64Stringand display it in an<img>element. Although typically you would save the uploaded file to the server and just save the path/display name in the db, and the include that in the view
– user3559349
Nov 14 '18 at 11:02
@StephenMuecke good point. I removed alreadyaktualnosciCreateVM.AktualnosciImage = aktualnosci.AktualnosciImage.ToArray();from my controller. Just right now I am gettingNullReferenceException: Object reference not set to an instance of an object.forawait aktualnosci.AktualnosciImage.CopyToAsync(memoryStream);. I need to figure out what I did wrong...
– Przemyslaw.Pszemek
Nov 14 '18 at 11:05
I cannot see anything in your question aboutawait aktualnosci.AktualnosciImage.CopyToAsync(memoryStream)- you will need to ask a new question about that
– user3559349
Nov 14 '18 at 11:08
You cannot set the value of a file input programatically (it can only be set by the user selecting a file in the browser), so it would be pointless to try and set the value of the
IFormFile property– user3559349
Nov 14 '18 at 10:30
You cannot set the value of a file input programatically (it can only be set by the user selecting a file in the browser), so it would be pointless to try and set the value of the
IFormFile property– user3559349
Nov 14 '18 at 10:30
@StephenMuecke In that case how can I pass img from database to my VM, if in VM I have
IFormFile type property, that is required to upload the file?– Przemyslaw.Pszemek
Nov 14 '18 at 10:54
@StephenMuecke In that case how can I pass img from database to my VM, if in VM I have
IFormFile type property, that is required to upload the file?– Przemyslaw.Pszemek
Nov 14 '18 at 10:54
2
2
IFormFile is for uploading a file only. Its not clear what you want. If you want to display an image in the view (to indicate what the user previously uploaded), then you can convert the byte to a Base64String and display it in an <img> element. Although typically you would save the uploaded file to the server and just save the path/display name in the db, and the include that in the view– user3559349
Nov 14 '18 at 11:02
IFormFile is for uploading a file only. Its not clear what you want. If you want to display an image in the view (to indicate what the user previously uploaded), then you can convert the byte to a Base64String and display it in an <img> element. Although typically you would save the uploaded file to the server and just save the path/display name in the db, and the include that in the view– user3559349
Nov 14 '18 at 11:02
@StephenMuecke good point. I removed already
aktualnosciCreateVM.AktualnosciImage = aktualnosci.AktualnosciImage.ToArray(); from my controller. Just right now I am getting NullReferenceException: Object reference not set to an instance of an object. for await aktualnosci.AktualnosciImage.CopyToAsync(memoryStream);. I need to figure out what I did wrong...– Przemyslaw.Pszemek
Nov 14 '18 at 11:05
@StephenMuecke good point. I removed already
aktualnosciCreateVM.AktualnosciImage = aktualnosci.AktualnosciImage.ToArray(); from my controller. Just right now I am getting NullReferenceException: Object reference not set to an instance of an object. for await aktualnosci.AktualnosciImage.CopyToAsync(memoryStream);. I need to figure out what I did wrong...– Przemyslaw.Pszemek
Nov 14 '18 at 11:05
I cannot see anything in your question about
await aktualnosci.AktualnosciImage.CopyToAsync(memoryStream) - you will need to ask a new question about that– user3559349
Nov 14 '18 at 11:08
I cannot see anything in your question about
await aktualnosci.AktualnosciImage.CopyToAsync(memoryStream) - you will need to ask a new question about that– user3559349
Nov 14 '18 at 11:08
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297803%2fhow-to-parse-byte-array-from-model-with-iformfile-from-viewmodel-for-file-uplo%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297803%2fhow-to-parse-byte-array-from-model-with-iformfile-from-viewmodel-for-file-uplo%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You cannot set the value of a file input programatically (it can only be set by the user selecting a file in the browser), so it would be pointless to try and set the value of the
IFormFileproperty– user3559349
Nov 14 '18 at 10:30
@StephenMuecke In that case how can I pass img from database to my VM, if in VM I have
IFormFiletype property, that is required to upload the file?– Przemyslaw.Pszemek
Nov 14 '18 at 10:54
2
IFormFileis for uploading a file only. Its not clear what you want. If you want to display an image in the view (to indicate what the user previously uploaded), then you can convert thebyteto aBase64Stringand display it in an<img>element. Although typically you would save the uploaded file to the server and just save the path/display name in the db, and the include that in the view– user3559349
Nov 14 '18 at 11:02
@StephenMuecke good point. I removed already
aktualnosciCreateVM.AktualnosciImage = aktualnosci.AktualnosciImage.ToArray();from my controller. Just right now I am gettingNullReferenceException: Object reference not set to an instance of an object.forawait aktualnosci.AktualnosciImage.CopyToAsync(memoryStream);. I need to figure out what I did wrong...– Przemyslaw.Pszemek
Nov 14 '18 at 11:05
I cannot see anything in your question about
await aktualnosci.AktualnosciImage.CopyToAsync(memoryStream)- you will need to ask a new question about that– user3559349
Nov 14 '18 at 11:08