我知道我在问一个愚蠢的问题。但我不是一个好的Google员工。我需要做的就是将文件从a上传到特定的相对地址。即“〜/ Content / img / uploads /”。这就是我到目前为止所做的:
<div id="my_camera"></div>
<!-- First, include the Webcam.js JavaScript Library -->
<script type="text/javascript" src="~/Content/js/plugins/WebCam/webcam.js"></script>
<!-- Configure a few settings and attach camera -->
<script language="JavaScript">
Webcam.set({
width: 320,
height: 240,
image_format: 'jpeg',
jpeg_quality: 90
});
Webcam.attach( '#my_camera' );
</script>
<!-- A button for taking snaps -->
<form>
<input type=button value="Take Snapshot" onClick="take_snapshot()">
</form>
<div id="results" style="float:right;width:320px;height:240px"></div>
<p id="myResult"></p>
<script language="JavaScript">
function take_snapshot() {
// take snapshot and get image data
Webcam.snap( function(data_uri) {
// display results in page
//Upload image here
} );
}
EDIT 这是我不明白的。这是我发现上传图片的功能,但没有指定将上传到的网址:
function UploadFile(file) {
var xhr = new XMLHttpRequest();
if (xhr.upload && file.type == "image/jpeg" && file.size <= $id("MAX_FILE_SIZE").value) {
// start upload
xhr.open("POST", $id("upload").action, true);
xhr.setRequestHeader("X_FILENAME", file.name);
xhr.send(file);
}
}
答案 0 :(得分:0)
所以我解决了我的问题。任何人都可以改进我的解决方案吗?
HTML:
<div id="my_camera" class="center-block">
</div>
<!-- First, include the Webcam.js JavaScript Library -->
<script type="text/javascript" src="~/Content/js/plugins/WebCam/webcam.js"></script>
<!-- Configure a few settings and attach camera -->
<script language="JavaScript">
Webcam.set({
width: 320,
height: 320,
image_format: 'jpeg',
jpeg_quality: 90
});
Webcam.attach( '#my_camera' );
</script>
<form>
<input type=button value="Take Snapshot" onClick="take_snapshot()">
</form>
<!-- A button for taking snaps -->
<input type="hidden" id="base64Label" name="base64Label" />
<p id="myResult"></p>
然后是JS:
var myResult = document.getElementById('base64Label');
function take_snapshot() {
Webcam.snap(function (data_uri) {
myResult.value = data_uri;
});
}
最后是我的C#:
public ActionResult ReportFault(Model model, FormCollection collection){
string base64 = collection.Get("base64Label").Substring(ExpectedImagePrefix.Length);
byte[] bytes = Convert.FromBase64String(base64);
string directory = "~/Content/img/faults/" + user.PublicUserId +"/";
fault.ImageData = bytes;
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
if (Directory.Exists(directory))
{
string date = (fault.DateCreated.Day.ToString() + fault.DateCreated.Month.ToString() + fault.DateCreated.Year.ToString() + fault.DateCreated.TimeOfDay.ToString().Replace(":", "")).Replace(".","");
image.Save(Server.MapPath(directory + date + ".jpg"));
fault.ImageMimeType = directory.Replace("~", "") + date + ".jpg";
}
else
{
string date = fault.DateCreated.Day.ToString() + fault.DateCreated.Month.ToString() + fault.DateCreated.Year.ToString() + fault.DateCreated.TimeOfDay.ToString().Replace(":","");
Directory.CreateDirectory(Server.MapPath(directory));
image.Save(Server.MapPath(directory+date+".jpg"));
fault.ImageMimeType = directory.Replace("~","") + date + ".jpg";
}
}}