Невозможно выяснить, как загрузить / загрузить файл в базу данных MVC form [duplicate]

В Android O и позже эта ошибка возникает, когда вы устанавливаете

 android:screenOrientation="portrait"

в манифесте.

Удалите эту строку и используйте

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

в вашей деятельности.

Это устранит вашу проблему.

13
задан tereško 27 February 2013 в 09:19
поделиться

1 ответ

Вы можете использовать byte[] в своей модели и HttpPostedFileBase в своей модели просмотра. Например:

public class MyViewModel
{
    [Required]
    public HttpPostedFileBase File { get; set; }
}

, а затем:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        byte[] uploadedFile = new byte[model.File.InputStream.Length];
        model.File.InputStream.Read(uploadedFile, 0, uploadedFile.Length);

        // now you could pass the byte array to your model and store wherever 
        // you intended to store it

        return Content("Thanks for uploading the file");
    }
}

и, наконец, на ваш взгляд:

@model MyViewModel
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
        @Html.LabelFor(x => x.File)
        @Html.TextBoxFor(x => x.File, new { type = "file" })
        @Html.ValidationMessageFor(x => x.File)
    </div>

    <button type="submit">Upload</button>
}
37
ответ дан Darin Dimitrov 4 September 2018 в 09:06
поделиться
Другие вопросы по тегам:

Похожие вопросы: