简体   繁体   中英

How to handle an audio file uploaded using PhoneGap in a c# mvc.net application controller?

I am using PhoneGap API to record audio files (.wav) from iOS devices. I want to upload the recorded audio file to the application server (an asp.net c# mvc application).

I'm using the 'upload' method provided by PhoneGap's FileTransfer object to upload the file to the server as shown in this documentation page .

Assuming that my controller will be something like http://myapp.com/Media/UploadAudio , I want to know how to handle the file upload in the server side controller (method code) so that I can save the file to the filesystem.

I've handled it :)

Here is the code I used:

[HttpPost]
    public JsonResult UploadAudio()
    {

        HttpFileCollectionBase Files = Request.Files;

        bool fileSaved = false;

       foreach (string h in Files.AllKeys)
       {
           if (Files[h].ContentLength > 0)
           {
               string fileName = Files[h].FileName; 
               int fileSize =Files[h].ContentLength;

               string serverPath = Path.Combine(Server.MapPath("..\\Your\\Favorite\\Location\\"));

               if (!Directory.Exists(serverPath))
               {
                   Directory.CreateDirectory(serverPath);
               }

               try
               {
                    //Get & Save the File
                    Request.Files.Get(h).SaveAs(serverPath + fileName);
                    fileSaved = true;
               }
               catch (Exception ex)
               { 

               }

           }
       }
        return Json(new {FileSaved = fileSaved});
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM