Abhimanyu
Abhimanyu

Reputation: 2213

Get FileStream from form posted file

I have a control on view page. When user selects the file and clicks on submit button this makes ajax call to upload the file on server. Unfortunately my server method accepts file path (like C:/Videos/1.mp4) to upload. This works great with string demoPath in the code below but I'm not sure how to get similar path when user selects in control. Due to sercurity reasons modern browsers not allows exposing paths. How to achieve this?

    [HttpPost]
    public async Task<JsonResult> Upload(string lectureId, string filepath)
    {
        for (int i = 0; i < Request.Files.Count; i++)
        {
            //// This works great
            //string demoPath = "C:/Users/abchi/Desktop/BigBuckBunny.mp4";


            var file = Request.Files[i];
            var fileName = Path.GetFileName(file.FileName);
            //var path = Path.Combine(Server.MapPath("~/User/"), fileName);
            //file.SaveAs(path);

            //await RunUploader(demoPath);
            await RunUploader(get_path_from_posted_file_or_request);
        }

        return Json(new { error = false, message = "Video uploaded." });
    }


    public async Task RunUploader(string filePath)
    {
        // :::::::
        using (var fileStream = new FileStream(filePath, FileMode.Open))
        {
            // ::::
        }
        // ::::::
    }

Upvotes: 2

Views: 3292

Answers (2)

Abhimanyu
Abhimanyu

Reputation: 2213

I asked my fellow dev to make necessary changes in public async Task RunUploader(string filePath) parameters. Said code was part of YouTube .NET samples for console apps. Now we are developing for web, in this case we can't pass full path. So they made following changes:

[HttpPost]
public async Task<JsonResult> Upload(string lectureId)
{
    for (int i = 0; i < Request.Files.Count; i++)
    {
        var file = Request.Files[i];
        Stream fileStream = file.InputStream;
        await Run(fileStream);
    }

    return Json(new { error = false, message = "Video uploaded." });
}

public async Task Run(Stream fileStream)
{
    // ::::::::::
    using (fileStream)
    {
        // ::::::
    }
    // ::::::::::
}

Now with this change everything started working.

Upvotes: 1

Bartosz Czerwonka
Bartosz Czerwonka

Reputation: 1651

I'm not sure this is expected because I did not quite understand.

Download the file path of the user's computer can not be - https://stackoverflow.com/a/15201258/4599089

but if you want to have access to the FileStream on your server:

File has InputStream and you can use this:

[HttpPost]
public async Task<JsonResult> Upload(string lectureId, string filepath)
{
    for (int i = 0; i < Request.Files.Count; i++)
    {
        var file = Request.Files[i];
        var fileName = Path.GetFileName(file.FileName);

        var path = Path.Combine(Server.MapPath("~/User/"), fileName);
        var fileStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
        file.InputStream.CopyTo(fileStream);
        fileStream.Close();

        await RunUploader(path); //path or stream
    }

    return Json(new { error = false, message = "Video uploaded." });
}


public async Task RunUploader(string filePath)
{
    // :::::::
    using (var fileStream = new FileStream(filePath, FileMode.Open))
    {
        // ::::
    }
    // ::::::
}

Upvotes: 1

Related Questions