简体   繁体   English

如何将音频文件上传到服务器

[英]how to upload audio file to server

I want to know if there is a way to upload files server-side mainly I want to upload audios in a specific folder I first tried to upload and download by js but it didn't work here is HTML我想知道是否有一种方法可以在服务器端上传文件,主要是我想在特定文件夹中上传音频我首先尝试通过js上传和下载,但在这里不起作用是HTML

    <body>
        
        <!-- <button type="button" class="btn-primary" data-toggle="modal" data-target="#myModal"> -->
            <img src="assets/upload-btn.png" alt="" type="button" class="btn-img" data-toggle="modal" data-target="#myModal">
          <!-- </button> -->
        
          <!-- The Modal -->
          <div class="modal fade" id="myModal">
            <div class="modal-dialog modal-sm">
              <div class="modal-content">
              
                <!-- Modal Header -->
                <div class="modal-header">
                  <h4 class="modal-title">Upload Audio</h4>
                  <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                
                <!-- Modal body -->
                <div class="modal-body">
                    <form action="save?">
                        <input type="file" name="Audio" id="upload-aud" accept="Audio/mp3">
                    </form>
                </div>
                
                <!-- Modal footer -->
                <div class="modal-footer">
                    <input class="btn-submit "type="submit" id="submit" value="Upload" data-dismiss="modal" onclick=save()> 
                </div>
              </div>
            </div>
          </div>
        </span>
    </body>

first of all, to upload a media file (image, video, audio) to server, you need a backend.首先,要将媒体文件(图像、视频、音频)上传到服务器,您需要一个后端。 meaning you need to write some backend code with languages like PHP or Nodejs or Python... .这意味着您需要使用 PHP 或 Nodejs 或 Python... 等语言编写一些后端代码。

in the client side, you need to send the file to backend.在客户端,您需要将文件发送到后端。 this can be done two ways.这可以通过两种方式完成。

one, use a simple form:一、使用简单的形式:

<form action="save.php" method="post" enctype="multipart/form-data">
     <input type="file" name="Audio" id="upload-aud" accept="Audio/mp3">
    <input type="submit" value="save file">
</form>

this form will send a media file with the name "Audio" to the save.php file which is your backend.此表单会将名为“Audio”的媒体文件发送到作为您的后端的 save.php 文件。

second method is to use javascript and fetch the file from the input and send an ajax request.第二种方法是使用 javascript 并从输入中获取文件并发送 ajax 请求。 you can get the file from the input like this:您可以像这样从输入中获取文件:

var file = document.getElementById('upload-aud').files[0]

now, the file variable contains your media file and you can use ajax to send it.现在, file变量包含您的媒体文件,您可以使用 ajax 发送它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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