0

So I am referring to Saving WAV File Recorded in Chrome to Server to save a blob to my server. However, I cannot translate this PHP sentence in to C#.

<?php
// get the temporary name that PHP gave to the uploaded file
$tmp_filename=$_FILES["that_random_filename.wav"]["tmp_name"];
// rename the temporary file (because PHP deletes the file as soon as it's done with it)
rename($tmp_filename,"/tmp/uploaded_audio.wav");
?>

Here is what I've tried so far

View

var fd = new FormData();
fd.append("that_random_filename.wav", blob);
xhr.open("POST", "SubmitSound", true);
xhr.send(fd);

View model

public class SoundBlob
{        
    public string key { get; set; }
    public HttpPostedFileBase blob { get; set; } // I tried byte[] and string too
}

Controller

       [HttpPost]
        public ActionResult SubmitSound(SoundBlob blob)
        {
            // Create the new, empty data file.
            string fileName = AppDomain.CurrentDomain.BaseDirectory + "/Content/Sound/" + Environment.TickCount + ".wav";
            FileStream fs = new FileStream(fileName, FileMode.CreateNew);
            BinaryWriter w = new BinaryWriter(fs);

            blob.blob.SaveAs(blob.key);
            w.Close();
            fs.Close();           
            return new JsonResult() { Data = "Saved successfully" };
        }

Both key and blob of SoundBlob is null! How can I fix it?

Community
  • 1
  • 1
Minh Triet
  • 1,190
  • 1
  • 15
  • 35

2 Answers2

1

Does not rely on a type of C#, rather using this script.

            var length = Request.ContentLength;
            var bytes = new byte[length];
            Request.InputStream.Read(bytes, 0, length);
Minh Triet
  • 1,190
  • 1
  • 15
  • 35
0

Try this:

Client:

<script src="http://code.jquery.com/jquery-1.10.2.min.js" ></script>
<div class='wrapper' >
    <input type='file' class='target' id='targetinput' />
</div>

Javascript:

var xhr = new XMLHttpRequest();
var fd = new FormData();
fd.append("blob.blob", document.getElementById('targetinput').files[0]);
xhr.open("POST", "/Home/SubmitSound", true);
xhr.send(fd);

Server:

[HttpPost]
public ActionResult SubmitSound(SoundBlob blob)
{
        var fileName = Server.MapPath(string.Format("/Content/Sound/{0}.wav", Environment.TickCount));
        blob.blob.SaveAs(fileName);
        return new JsonResult { Data = "Saved successfully" };
}

Perfectly works, i just checked it out

testCoder
  • 7,155
  • 13
  • 56
  • 75
  • My apologize, I do not provide enough background information. Basically, I am using a javascript library to record a file, when the recording is done, a `blob` is passed to a function to save it to the client machine. If you view the source of http://webaudiodemos.appspot.com/AudioRecorder/index.html, you can see the function `doneEncoding`, which is what I am trying to modify. – Minh Triet Sep 23 '13 at 10:08
  • What I am trying to achieve is user can click on a button, record their voice, and click on the same button again, then the `blob` can be uploaded to the database. Your solution requires people to select a file, I am afraid. – Minh Triet Sep 23 '13 at 10:14
  • This did it for me. Great job. – Matt Dec 14 '13 at 05:05