如何在服务器中保存图像

时间:2019-05-21 12:23:58

标签: reactjs

我正在尝试将图像保存在数据库中,但是当我执行以下操作时,它将图像保存为dataURL,但是该URL以localhost开头,如何防止它出现?我使用React作为前端。

uploadImage = (event) => {
    var imgSize = event.target.files[0].size;

    $('#img').attr('hidden', false);
    if (imgSize < 1000000 ) {
        this.setState({
            image: URL.createObjectURL(event.target.files[0]),
            imageSize: imgSize
        });
        document.getElementById("errImgUpload").innerHTML = "";
    }
    else {
        document.getElementById("errImgUpload").innerHTML = "Maximum image size 1Mb";
    }
}
    
 <div className="form-group">
   <label for="file-upload" className="custom-file-upload">
     <span className="fa fa-upload"></span> Upload image
   </label>
   <input onChange={(event) => this.uploadImage(event)} name="file-upload" id="file-upload" type="file" accept="image/*" />
   <span id="errImgUpload" className="text text-danger"></span>
</div>

斑点是http://localhost:10002/b46e96f5-83ce-4d10-b668-2bd038721b5a,什么是斑点?

2 个答案:

答案 0 :(得分:0)

URL.createObjectURL()创建一个blob,它是内存中文件的二进制表示形式。它不会上传文件。我不确定您从哪里得到代码。您可能想在MDN上阅读有关此内容的更多信息。

上载需要后端服务来打开一个端点,以便您将帖子数据发送到该端点。您需要使用<input type='file'>标记将文件作为表单数据发送,并将服务端点设置为表单中的url。从this article开始。

答案 1 :(得分:0)

您需要使用Option Explicit Public Sub TestDeleteAllAllowed() lastRowC = ws.Cells(ws.Rows.Count, 3).End(xlUp).Row + 1 ' bottom populated cell of Column "C", plus 1 'your import stuff here DeleteAllAllowed StartRow:=lastRowC End Sub Public Sub DeleteAllAllowed(Optional StartRow As Long = 1) Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Report") 'define worksheet Dim LastRow As Long LastRow = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row 'find last used row in column F If LastRow < StartRow Then Exit Sub Dim DataArray As Variant 'read column F into array DataArray = ws.Columns("F").Resize(RowSize:=LastRow - StartRow + 1).Offset(RowOffset:=StartRow - 1).Value 'column F contains "allowed" Dim RowsToDelete As Range 'we collect all rows to delete here If IsArray(DataArray) Then Dim iRow As Long For iRow = 1 To LastRow - StartRow + 1 If DataArray(iRow, 1) = "allowed" Then If RowsToDelete Is Nothing Then Set RowsToDelete = ws.Rows(iRow + StartRow - 1) Else Set RowsToDelete = Union(RowsToDelete, ws.Rows(iRow + StartRow - 1)) End If End If Next iRow Else If DataArray = "allowed" Then Set RowsToDelete = ws.Rows(LastRow) End If End If If Not RowsToDelete Is Nothing Then RowsToDelete.Delete 'delete all rows at once End If End Sub 发布图像数据,如下所示:

FormData

blob是Binary Large Object,您可以在MDN中找到更多详细信息。