0

我必须从一个网址制作一个图像项目。我正在上传一张图片并显示它。我正在努力将网址变成图像。

我得到的错误是;

[object%20File]:1 GET file:///D:/Uni/Uni/TM284/TMA%2003/TMA%2003%20zip%20file-20210209/[object%20File] net::ERR_FILE_NOT_FOUND

我的代码;

function addImageEntry(key, url) {
// Create a image element
var imgElement = new Image();
imgElement.alt = "Photo entry";

// Load the image
imgElement.src = url;

// Add a section to the page containing the image
addSection(key, imgElement);
}

function addEntryClick() {
// Add an empty text entry, using the current timestamp to make a key
var key = "diary" + Date.now();
var text = "";
var isNewEntry = true;
addTextEntry(key, text, isNewEntry);
}

function addPhotoClick() {
// Trigger the 'click' event of the hidden file input element
// (as demonstrated in Block 3 Part 4)
var inputElement = document.querySelector("#image input");
inputElement.click();
}

function processFile(event) {
// Create an event listener to add an image entry
function addImage(url) {
    // Add a new image entry, using the current timestamp to make a key
    var key = "diary" + Date.now();
    addImageEntry(key, url);
    
    // TODO: Q1(c)(iv) Task 1 of 2
    // Make an image item using the given url
    // (demonstrated elsewhere in this file)                     
    
    var data = fileObject;
            
            
    // Store the item in local storage using the given key
    // (demonstrated elsewhere in this file)
    var item = makeItem("image", data);
    localStorage.setItem(key, item);
}

// Add a 'dummy' image entry 
//addImage(window.DUMMY_DATA_URL);

// TODO: Q1(c)(iv) Task 2 of 2
// Complete this function to read a file when it is selected:
// (reading files into a data URL using FileReader is demonstrated in Block 3 Part 4)
// (imgElement and messageElement do not exist in this app so remove that code)
// ...then call addImage using the data URL you obtain
// ...and comment out line above using window.DUMMY_DATA_URL

var reader = new FileReader();  
    
var img = event.target;
var fileObject = img.files[0];
reader.readAsDataURL(fileObject);

addImage(fileObject);



 
// Clear the file selection (allows selecting the same file again)
//inputElement.value = '';
 }
4

0 回答 0