I'm having a bit of trouble solving an issue with imaging uploading. I'm using an html file upload form with a jquery function that checks the file type of the image being selected. What I want to do is replace the current image on a web site page with whatever the user decides to upload. However, I cannot find a way to get the full image source path from the upload--I always get a fake path, which doesn't work for what I want to do. Do I really need to create a database for images if I want to do this, or is there some other way? I can't use PHP/MySQL code--only ASP.NET, C#, and SQL/SQL Express.
我在解决图像上传的问题上有点困难。我使用的是html文件上传表单,带有jquery函数,用于检查所选图像的文件类型。我想要做的是用用户决定上传的内容替换网站页面上的当前图像。但是,我找不到从上传中获取完整图像源路径的方法——我总是得到一个伪路径,这对我想要做的事情不起作用。我真的需要为图片创建一个数据库吗?我不能使用PHP/MySQL代码,只能使用ASP。NET、c#和SQL/SQL Express。
Here is the code I have so far, which works except I can't successfully replace the default image (which has name and id of "logo) with an uploaded one since I don't get the full path from the uploader. Any suggestions would be very helpful! Thanks in advance!
这是我到目前为止的代码,除了我不能成功地将默认的图像(有“logo”的名称和id)替换为上传的图像(因为我没有从上传者获得完整的路径)。任何建议都很有帮助!提前谢谢!
<html>
<head>
<title>Image Swap Widget Testing</title>
<style type="text/css">
.myWebForm{
border: 4px solid grey;
width: 300px;
height: 200px;
padding: 10px;
font-family: Arial;
font-size: small;
}
</style>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('INPUT[type="file"]').change(function () {
var ext = this.value.match(/\.(.+)$/)[1];
switch (ext) {
case 'jpg':
case 'jpeg':
case 'png':
case 'gif':
case 'bmp':
$('#uploading').attr('disabled', false);
$('#logo').attr("src", $('#uploading').val());
alert('image approved!');
alert($('img').attr('src'));
alert($('img')[0].src);
alert($('#uploading').val(img.src));
break;
default:
alert('This is not an allowed file type.');
this.value = '';
}
});
$('#addButton').click(function (){
//alert('Add Image Button was clicked');
//var newImgSrc = getSrc($('#uploading').val());
//$('#logo').attr("src", newImgSrc);
//alert($('#logo').attr("src"));
//alert($('#uploading').val());
});
});
</script>
</head>
<body>
<img src="Lighthouse.jpg" name="logo" alt="logo" id="logo" width="200" />
<br/>
<form name="myWebForm" class="myWebForm" action="#" method="post">
<h3>Change Default Logo</h3>
<input type="file" name="uploadField" id="uploading" />
<p><b>Allow extensions:</b> .png, .bmp, .jpg, .jepg, .gif<p>
<p><b>Dimensions:</b> 50px x 80px<p>
<input type="button" name="addButton" id ="addButton" class="addButton" value="+ Add" />
<input type="button" name="cancelButton" id ="cancelButton" class="cancelButton" value="X Cancel" />
</body>
</html>
2 个解决方案
#1
6
You can always use the FileSystem API. First, our input:
您总是可以使用文件系统API。首先,我们的输入:
<input type="file" id="MyImage" />
Listen for the change and grab the file:
聆听更改并获取文件:
document.getElementById('MyImage').onchange = function(e) {
// Get the first file in the FileList object
var imageFile = this.files[0];
// get a local URL representation of the image blob
var url = window.URL.createObjectURL(imageFile);
// Now use your newly created URL!
someImageTag.src = url;
}
Hope that helps!
希望会有帮助!
#2
1
If you use iframe transport you can get the path from iframe when it's loaded.
如果您使用iframe传输,则可以从iframe加载时获取路径。
<script>
$('#upload-frame').load(function(e) {
$('img').attr('src', $(this).contents().find('body').html());
});
</script>
<form method="post" action="upload.php" enctype="multipart/form-data"
id="upload-form" target="upload-frame">
<input type="file" name="uploadField" id="uploading" />
<input type="submit" value="upload"/>
</form>
<iframe id="upload-frame" name="upload-frame"></iframe>
on server you can do something like:
在服务器上,您可以执行以下操作:
$info = pathinfo(basename($_FILES['image']['name']));
$ext = strtolower($info['extension']);
if (in_array($ext, array('png', 'jpg', 'gif', 'bmp'))) {
$path = 'SOME/PATH/filename.' . $ext;
if (move_uploaded_file($_FILES['image']['tmp_name'], $path)) {
echo $path;
}
}
#1
6
You can always use the FileSystem API. First, our input:
您总是可以使用文件系统API。首先,我们的输入:
<input type="file" id="MyImage" />
Listen for the change and grab the file:
聆听更改并获取文件:
document.getElementById('MyImage').onchange = function(e) {
// Get the first file in the FileList object
var imageFile = this.files[0];
// get a local URL representation of the image blob
var url = window.URL.createObjectURL(imageFile);
// Now use your newly created URL!
someImageTag.src = url;
}
Hope that helps!
希望会有帮助!
#2
1
If you use iframe transport you can get the path from iframe when it's loaded.
如果您使用iframe传输,则可以从iframe加载时获取路径。
<script>
$('#upload-frame').load(function(e) {
$('img').attr('src', $(this).contents().find('body').html());
});
</script>
<form method="post" action="upload.php" enctype="multipart/form-data"
id="upload-form" target="upload-frame">
<input type="file" name="uploadField" id="uploading" />
<input type="submit" value="upload"/>
</form>
<iframe id="upload-frame" name="upload-frame"></iframe>
on server you can do something like:
在服务器上,您可以执行以下操作:
$info = pathinfo(basename($_FILES['image']['name']));
$ext = strtolower($info['extension']);
if (in_array($ext, array('png', 'jpg', 'gif', 'bmp'))) {
$path = 'SOME/PATH/filename.' . $ext;
if (move_uploaded_file($_FILES['image']['tmp_name'], $path)) {
echo $path;
}
}