コードを実行しようとすると、コールバック メソッドによってサーバーに画像を保存しようとしています。例外が発生します。 ASP-NET-2-0-C
ストリーム s = File.OpenRead(returnValue); csファイルで、例外ファイルが見つかりませんでした
<input id="File1" runat="server" onchange="PopulateList(this)" name="File1" type="File" />
<script language="javascript" type="text/javascript">
var counter = 1;
var newPath = '';
var filePath = '';
function PopulateList(obj) {
// Upload the image to the server folder
filePath = obj.value;
// calls the server's method using client callbacks
CallServer(obj.value, '');
}
function ReceiveServerData(rValue) {
// The new path will contain the path of the image which is inside the server's folder
newPath = rValue;
alert(newPath);
//CreateNestedElements();
}
</script>
Cs ファイル内
protected string returnValue = String.Empty;
protected void Page_Load(object sender, EventArgs e)
{
string[] filePaths = null;
//HtmlInputHidden hiddenControl = (HtmlInputHidden)Page.FindControl("list");
//if (!String.IsNullOrEmpty(hiddenControl.Value))
//{
// filePaths = hiddenControl.Value.Split('|');
// //SaveFilesToDB(filePaths);
//}
// register the callback script
string sbReference = ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
string cbScript = String.Empty;
// check if the script is already registered or not
if (!ClientScript.IsClientScriptBlockRegistered("CallServer"))
{
cbScript = @" function CallServer(arg,context) { " + sbReference + "}";
ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", cbScript, true);
}
}
public string GetCallbackResult()
{
string fileName = System.IO.Path.GetFileName(returnValue);
string path = Server.MapPath("Images/");
string fullPath = path + fileName;
Stream s = File.OpenRead(returnValue);
byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, (int)s.Length);
int len = (int)s.Length;
s.Dispose();
s.Close();
FileStream fs = new FileStream(fullPath, FileMode.Create);
fs.Write(buffer, 0, len);
Bitmap bmp = new Bitmap(fs);
if (System.IO.Path.GetExtension(returnValue).Equals(".gif"))
{
bmp.Save(fs, ImageFormat.Gif);
}
else
{
bmp.Save(fs, ImageFormat.Jpeg);
}
bmp.Dispose();
fs.Dispose();
fs.Close();
return "Images/" + fileName;
}
public void RaiseCallbackEvent(string eventArgument)
{
if (!String.IsNullOrEmpty(eventArgument))
{
returnValue = eventArgument;
}
}