Home > DeveloperSection > Forums > Asp.NET temporary file saving
jacob rasel
jacob rasel

Total Post:88

Points:616
Posted on    March-31-2015 5:43 AM

 ASP.Net C#  .NET  ASP.NET MVC 
Ratings:


 1 Reply(s)
 624  View(s)
Rate this:

I got a path from the jquery code URL.createObjectURL(event.target.files[0]);

 

It returns something like this : blob:http%3A/localhost%3A59105/f7dae0f7-088f-48cf-b446-eeda0bf23705

 

I tried to save this file like

 

byte[] data;

    using (WebClient client = new WebClient())

    {

        data = client.DownloadData("blob:http%3A/localhost%3A59105/f7dae0f7-088f-48cf-b446-eeda0bf23705");

    }

    File.WriteAllBytes(@"~/a.jpg", data);

But it gives an error about the code above:

 

  The URI prefix is not recognized.

How exactly I can copy this file?

 

Thanks for your suggestions.



lois waisbrooker
lois waisbrooker

Total Post:70

Points:494
Posted on    March-31-2015 7:02 AM

1.Create simple GET method

 

[HttpGet]

public ActionResult GetFile(){

  return View();

}

2. Create View with @Html.BeginForm helper

 

@using (Html.BeginForm("GetFile","YourController", FormMethod.Post, { enctype = "multipart/form-data" }))

{

<input type="file" id="fileup" name="file"/>

<input type="submit" value="Send">

}

Rembember to use name attribute and overloaded version of Html.BeginForm()

 

3.Get data in Backend

 

[HttpPost]

public ActionResult GetFile(HttpPostedFileBase file)

{

  if (file != null && file.ContentLength > 0)

  {

     var fileName = Path.GetFileName(file.FileName);

     var filePath = Path.Combine(Server.MapPath("~/Temp/"), fileName);

     file.SaveAs(path);

  }

 

  return RedirectToAction("Success");

}

Name in html attribute must have same name as HttpPostedFileBase.


Don't want to miss updates? Please click the below button!

Follow MindStick