2017-05-30 89 views
0

我想使用Javascript访问从浏览器上传的文件的网址。你可以帮我吗?访问上传文件的网址

fileUpload = function() { 
 
     var fileUpload = document.getElementById("fileUpload"); 
 
     
 

 
     guid = (S4() + S4() + "-" + S4() + "-4" + S4().substr(0, 3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase(); 
 
     console.log(guid); 
 

 
     
 
    }
<div class="col-md-4 fileUpload btn btn-primary"> 
 
       <span>Dosya Yükle</span> 
 
       <input type="file" class="upload" name="fileToUpload" id="fileUpload" onchange="fileUpload()" /> 
 
      </div>

+0

S4没有定义。你缺少S4功能。 –

+0

目前 - 您没有上传任何文件...并且上传的文件没有自动获取URL,服务器必须提供一个。 – Quentin

+0

当您上传文件时,您必须在服务器端对其进行管理,将其放在某处,设置访问权限等等。同样,在上传之后,您必须告诉客户端文件位于服务器上的位置。 – Cossintan

回答

0

您可以使用

document.querySelector('.upload').addEventListener('change',function(){ 
 
    var fileUrl = this.value, 
 
\t \t \t \t fileName = this.files[0].name; 
 
     alert('Your url is '+ fileUrl) 
 
     alert('Your file name is '+ fileName) 
 
    });
.file_upload { 
 
\t margin-bottom: 30px; 
 
\t position: relative; 
 
\t cursor: pointer; 
 
\t background: #73af27; 
 
\t display: inline-block; 
 
\t height: 40px; 
 
\t line-height: 40px; 
 
\t color: #fff; 
 
\t padding: 0 15px; 
 
\t text-align: center; 
 
\t -moz-transition: all 0.3s 0.03s ease; 
 
\t -o-transition: all 0.3s 0.03s ease; 
 
\t -webkit-transition: all 0.3s 0.03s ease; 
 
\t border: 0; 
 
} 
 
.file_upload [type="file"] { 
 
\t position: absolute; 
 
\t top: 0; 
 
\t right: 0; 
 
\t bottom: 0; 
 
\t left: 0; 
 
\t visibility: hidden; 
 
\t opacity: 0; 
 
}
<div class="file-upload"> 
 
    <label class="file_upload" data-error="This format is not correct"> 
 
    <span>Attach CV</span> 
 
    <input type="file" name="file" class="upload" accept="aplication/pdf, aplication/doc, aplication/rtf, aplication/docx"> 
 
    </label> 
 
</div>

+0

谢谢假路径 – adabusra

+0

注意:变量'fileUrl'没有任何URL。它是一条看起来像一条路径的字符串,但它也完全是假的(因此在任何地方都没有用处) – Kaiido