English 中文(简体)
阅读“档案”物体的内容?
原标题:Read the contents of a "file" object?

因此,我有一个“File”物体(通过处理文件拖拉和从桌面上 drop出)。 我可以向服务器寄送文件,然后将文件退回,以备处理。 但是,如果不这样做,能否阅读其内容?

Here, play around with this fiddle. Drag any file to the box and use the variable file.

我已经尝试了这个目标的所有方法。 你们能否拿到你刚被拖入浏览器的文件内容?

PS:我向服务器发送文件,如:

var ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("returnRawPostData.php");
ajaxRequest.send(file);

我可能已经错过上述法典中的内容,但这只是因为有人不使用简明的JAJAX电话。

最佳回答

利用Martin Mally链接 (胜诉) 我来到这里:

var file = e.dataTransfer.files[0],
    read = new FileReader();

read.readAsBinaryString(file);

read.onloadend = function(){
    console.log(read.result);
}

保存档案内容。

问题回答

I think it s possible; check these two articles:

  1. https://developer.mozilla.org/en/Using_files_from_web_applications
  2. http://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/

这两架飞机在上载到服务器之前,都通过联合材料/超文本使用“透射”文档。 (如图片转录等) 我希望它能够提供帮助。

现代浏览器现在可以使用<条码>Blob.text(的APIC,该文本提供了一种基于承诺的阅读文档方法。

文档List(inputElement.files)内的每一项目都是Blob的事例,意思是,我们可以将文件List内任何项目的.text(改为案文。

如果你熟悉《同义法典》和诺言的话,这并不像建立档案和运用事件那样简单。

async function readTextFromFileInput(input: HTMLInputElement): Promise<string | void> {
  if (input.files?.length !== 1) {
    // No file selected...
    return;
  }

  return await input.files[0].text();
}

我刚才的讲话

let reader = new FileReader();
reader.readAsText(your_file_object);

reader.onload = function() {
    console.log(reader.result);  // prints file contents
};




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签