在这篇文章中,我们将带领您了解Php:将blob转换为图像文件的全貌,包括phpblob的相关情况。同时,我们还将为您介绍有关Angular:将Blob转换为pdf、BLOB转换为字符串或图像/PHP
在这篇文章中,我们将带领您了解Php:将blob转换为图像文件的全貌,包括php blob的相关情况。同时,我们还将为您介绍有关Angular:将Blob转换为pdf、BLOB转换为字符串或图像/ PHP或SQL、c# – 将blob转换回原始文件类型并使其可供下载、javascript – 同步将Blob转换为二进制字符串的知识,以帮助您更好地理解这个主题。
本文目录一览:- Php:将blob转换为图像文件(php blob)
- Angular:将Blob转换为pdf
- BLOB转换为字符串或图像/ PHP或SQL
- c# – 将blob转换回原始文件类型并使其可供下载
- javascript – 同步将Blob转换为二进制字符串
Php:将blob转换为图像文件(php blob)
解决方法
在某些奇怪的操作系统上,您必须为输出文件提供相应的扩展名,以便可以识别图像文件.
Angular:将Blob转换为pdf
我创建了一个SPRING BOOT服务,可以存储不同类型的文件。当我尝试将这项服务使用到ANGULAR时,图像也正常工作,但是当我尝试使用ng-pdf-
viewer显示pdf文件时,它对我不起作用。
我的component.ts:
export class AppComponent {
constructor(private httpClient: HttpClient) {}
tag: string;
selectedFile: File;
retrievedFile: any;
base64Data: any;
retrieveResonse: any;
message: string;
UserTag: any;
//Gets called when the user selects a file
public onFileChanged(event) {
//Select File
this.selectedFile = event.target.files[0];
}
//当用户单击提交以上传文件时被调用
onUpload() {
console.log(this.selectedFile);
// FormData API提供了方法和属性,使我们可以轻松地准备要通过POST HTTP请求发送的表单数据。
const uploadImageData = new FormData();
uploadImageData.append("file",this.selectedFile,this.selectedFile.name);
uploadImageData.append("tag",this.tag);
//Make a call to the Spring Boot Application to save the file
this.httpClient
.post("http://localhost:8080/do",uploadImageData,{
observe: "response"
})
.subscribe(response => {
if (response.status === 200) {
this.message = "Image uploaded successfully";
} else {
this.message = "Image not uploaded successfully";
}
});
}
//当用户单击“检索文件”按钮以从后端获取图像时被调用
getFile() {
//Make a call to Spring Boot to get the file Bytes.
this.httpClient
.get("http://localhost:8080/get/" + this.UserTag)
.subscribe(res => {
this.retrieveResonse = res;
this.base64Data = this.retrieveResonse.fileContent;
if (
this.retrieveResonse.fileType == "jpg" ||
this.retrieveResonse.fileType == "png" ||
this.retrieveResonse.fileType == "jpeg"
) {
this.retrievedFile = "data:image/jpg;base64," + this.base64Data;
}
if (this.retrieveResonse.fileType == "pdf") {
var blob = new Blob([this.base64Data],{ type: "application/pdf" });
this.retrievedFile = blob;
}
});
}
}
get方法:
public DBFile getFile( String fileTag) throws IOException {
final Optional<DBFile> retrievedFile = fileRepo.findByFileTag(fileTag);
DBFile img = new DBFile(retrievedFile.get().getName(),retrievedFile.get().getFileType(),decompressBytes(retrievedFile.get().getFileContent()),retrievedFile.get().getAddedAt(),retrievedFile.get().getFileTag());
decompress方法://解压缩文件字节,然后再将其返回给angular应用程序
public static byte[] decompressBytes(byte[] data) {
Inflater inflater = new Inflater();
inflater.setInput(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
byte[] buffer = new byte[1024];
try {
while (!inflater.finished()) {
int count = inflater.inflate(buffer);
outputStream.write(buffer,count);
}
outputStream.close();
} catch (IOException ioe) {
} catch (DataFormatException e) {
}
return outputStream.toByteArray();
}
return img;
}
我的组件。HTML
<div>
<div>
<h1>Upload Image</h1>
</div>
</div>
<div>
<div>
<input type="file" (change)="onFileChanged($event)" />
</div>
<div>
<div>
<label for="form">tag</label>
<input
type="text"id="tag"
[(ngModel)]="tag"
required
/>
</div>
</div>
<div>
<input type="button" (click)="onUpload()" value="upload" />
</div>
</div>
<hr />
<div>
<div>
<div *ngIf="message">{{ message }}</div>
</div>
</div>
{{ this.retrieveResonse | json }}
<div>
<div>
<input
type="text"id="name"
placeholder="File Tag"
[(ngModel)]="UserTag"
name="name"
/>
</div>
<div>
<input type="button" (click)="getFile()" value="Get File" />
</div>
</div>
<div>
<div>
<div>
<pdf-viewer
[src]="retrievedFile"
[render-text]="true"></pdf-viewer>
</div>
</div>
</div>
控制台错误:控制台错误的 照片
有需要帮助的人吗?..
BLOB转换为字符串或图像/ PHP或SQL
我有一个数组,在第3维上给了我一个图像斑点.
但是我需要图像,我尝试使用sql
<?PHP $global = \Database::getInstance()->execute('SELECT CONVERT(VarChar(40), image)
FROM mm_product2
WHERE id = '.$referenzen['id'])->fetchAllAssoc(); ?>
<?PHP print_r($global);?>
它显示此错误:
Fatal error: Uncaught exception Exception with message Query error:
You have an error in your sql Syntax; check the manual that corresponds
to your MysqL server version for the right Syntax to use near 'VarChar(40),
image) FROM mm_product2 WHERE id = 23' at line 1 (SELECT CONVERT(VarChar(40),
image) FROM mm_product2 WHERE id = 23
没有表达式,它可以工作,但我又像在标准数组中那样得到那个blob值:
[image] => 85392c33-a0f7-11e4-acb9-08606e695836 )
我可以知道如何在PHP或仅在sql中转换该值吗?
解决方法:
使用mySQL查询获取变量中的blob数据,然后使用它
'<img src="data:image/jpeg;base64,' . base64_encode($image) . '" width="200" height="200">'
c# – 将blob转换回原始文件类型并使其可供下载
这个blob数据实际上是一个存储在MysqL数据库中的小文件(小于100kb).我希望我的客户端应用程序能够将此blob转换回其原始文件类型,然后询问用户下载权限.现在我想知道这是不是一个好主意?或者我应该只在我的Web服务中进行文件转换,然后将文件本身发送到JSP应用程序?
任何帮助/建议将受到高度赞赏!如果你有任何好的教程/代码,这可能会帮助我进行转换,那么请在这里作为答案发布?提前致谢!
解决方法
sqlConnection pubsConn = new sqlConnection("Data Source=localhost;Integrated Security=sspI;Initial Catalog=pubs;"); sqlCommand logoCMD = new sqlCommand("SELECT pub_id,logo FROM pub_info",pubsConn); FileStream fs; // Writes the BLOB to a file (*.bmp). BinaryWriter bw; // Streams the BLOB to the FileStream object. int bufferSize = 100; // Size of the BLOB buffer. byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be filled by GetBytes. long retval; // The bytes returned from GetBytes. long startIndex = 0; // The starting position in the BLOB output. string pub_id = ""; // The publisher id to use in the file name. // Open the connection and read data into the DataReader. pubsConn.open(); sqlDataReader myReader = logoCMD.ExecuteReader(CommandBehavior.SequentialAccess); while (myReader.Read()) { // Get the publisher id,which must occur before getting the logo. pub_id = myReader.GetString(0); // Create a file to hold the output. fs = new FileStream("logo" + pub_id + ".bmp",FileMode.OpenorCreate,FileAccess.Write); bw = new BinaryWriter(fs); // Reset the starting byte for the new BLOB. startIndex = 0; // Read the bytes into outbyte[] and retain the number of bytes returned. retval = myReader.GetBytes(1,startIndex,outbyte,bufferSize); // Continue reading and writing while there are bytes beyond the size of the buffer. while (retval == bufferSize) { bw.Write(outbyte); bw.Flush(); // Reposition the start index to the end of the last buffer and fill the buffer. startIndex += bufferSize; retval = myReader.GetBytes(1,bufferSize); } // Write the remaining buffer. bw.Write(outbyte,(int)retval - 1); bw.Flush(); // Close the output file. bw.Close(); fs.Close(); } // Close the reader and the connection. myReader.Close(); pubsConn.Close();
javascript – 同步将Blob转换为二进制字符串
所以我认为正确的方法是将canvas tu dataURL,dataURL转换为blob和blob转换为二进制字符串.
理论上应该可以跳过blob,但我不知道为什么.
所以这就是我做的:
function copy(event) { console.log("copy"); console.log(event); //Get DataTransfer object var items = (event.clipboardData || event.originalEvent.clipboardData); //Canvas to blob var blob = Blob.fromDataURL(_this.editor.selection.getSelectedImage().toDataURL("image/png")); //File reader to convert blob to binary string var reader = new FileReader(); //File reader is for some reason asynchronous reader.onloadend = function () { items.setData(reader.result,"image/png"); } //This starts the conversion reader.readAsBinaryString(blob); //Prevent default copy operation event.preventDefault(); event.cancelBubble = true; return false; } div.addEventListener('copy',copy);
但是当在粘贴事件线程之外使用DataTransfer对象时,setData不再有任何生效的机会.
如何在同一个函数线程中进行转换?
解决方法
function blobToUint8Array(b) { var uri = URL.createObjectURL(b),xhr = new XMLHttpRequest(),i,ui8; xhr.open('GET',uri,false); xhr.send(); URL.revokeObjectURL(uri); ui8 = new Uint8Array(xhr.response.length); for (i = 0; i < xhr.response.length; ++i) { ui8[i] = xhr.response.charCodeAt(i); } return ui8; } var b = new Blob(['abc'],{type: 'application/octet-stream'}); blobToUint8Array(b); // [97,98,99]
你应该考虑让它保持异步但是让它成为两个阶段,因为你最终可能会锁定浏览器.
此外,您可以通过包含二进制安全的Base64解码器完全跳过Blob,您可能不需要通过Base64 AND Blob,只需其中一个.
关于Php:将blob转换为图像文件和php blob的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Angular:将Blob转换为pdf、BLOB转换为字符串或图像/ PHP或SQL、c# – 将blob转换回原始文件类型并使其可供下载、javascript – 同步将Blob转换为二进制字符串等相关内容,可以在本站寻找。
本文标签: